Hello, everyone!
Lead developer of Redshift Arena here.
I have this script that doesn’t return any errors, but it currently does nothing. It’s located under StarterPlayerScripts, and is intended to make every pickup on the map rotate and bob up & down like in 1990s FPS games. Any help would be greatly appreciated!! It should also be noted that the “Model” inside of each pickup will be destroyed upon being picked up. This script is a modified version of a server-sided one I created that went into each model, which created issues with performance.
local rotX = 0
local rotY = 4
local rotZ = 0
local delayVAR = 0
while wait(delayVAR) do
posX = 0
posY= script.upDownController.Value
posZ = 0
for i, v in pairs(game.Workspace.Entities.Pickups:GetChildren()) do
local item = v:FindFirstChild("Model")
if item == nil then return end
local ME = v.Model:FindFirstChild("CROSSY")
if ME == nil then return end
v.Model:SetPrimaryPartCFrame(v.Model.CROSSY.CFrame*
CFrame.new(
0 * posX,
1 * posY,
0 * posZ
)*
CFrame.Angles(
math.rad(rotX),
math.rad(rotY),
math.rad(rotZ)
)
,.5)
end
end
So the problem with this script is when you return it stops your script from running.
if item == nil then return end
This does not just stop the current iteration of the loop but the entire script, since it is not in a function.
E.g
for count = 1, 10 do
if count % 2 == 0 then return end
print(count)
end
That code will print 1 then exit.
To fix this, you can put the body of your loop into a function and then that function can decide whether to process an item or return.
function processNumber(n)
if n % 2 == 0 then return end
print(n)
end
for count = 1, 10 do
processNumber(count)
end
This code will print every odd number between 1 and 10.
2 Likes
So is it still possible to have this work when a pickup is destroyed (picked up) and then respawned shortly after?
I’m not entirely sure if I understand what i’m looking at and if it would help any with having the rotate / bobbing in a while loop (whilst keeping track of each item in-game.)
Of course it is possible to have this work when a pickup is destroyed and respawned. I presume that means the model instead the pickup would not exist so that pickup would be skipped.
The reason why what I posted above helps is because your code is using the return keyword incorrectly and this is likely why your code doesn’t work correctly. My post explains what the return keyword does in your case and what needs to be done to fix it.
Applied to your original code (I also changed style a little):
local rotX = 0
local rotY = 4
local rotZ = 0
local delayVAR = 0
function processPickup(pickup, posX, posY, posZ)
local item = pickup:FindFirstChild("Model")
if item == nil then return end
local ME = item:FindFirstChild("CROSSY")
if ME == nil then return end
local rotationCFrame = CFrame.Angles(math.rad(rotX), math.rad(rotY), math.rad(rotZ))
local upDownCFrame = CFrame.new(0 * posX, 1 * posY, 0 * posZ)
item:SetPrimaryPartCFrame(ME.CFrame * upDownCFrame * rotationCFrame)
end
while wait(delayVAR) do
for i, v in pairs(game.Workspace.Entities.Pickups:GetChildren()) do
processPickup(v, 0, script.upDownController.Value, 0)
end
end
Think about the control flow of this code. We have a function that processes each pickup and decides if something needs to be done to that pickup. If that pickup is not currently relevant, it returns from the function early. This works while your original code does not because when you return in your original code, you stop the entire script.
Another way of handling this problem would just be nesting your if statements like:
while wait(delayVAR) do
for i, v in pairs(game.Workspace.Entities.Pickups:GetChildren()) do
local item = v:FindFirstChild("Model")
if item ~= nil then
local ME = item:FindFirstChild("CROSSY")
if ME ~= nil then
---rotate item
end
end
end
end
Here we reverse the if statements so we check if the item is not nil instead.
2 Likes
Thank you so much!
You solved a problem that I was having for two days or so. I feel a bit silly though since I was pretty close to the solution you gave me - but it broke when pickups spawned. Still though, thank you. 