Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

trying to get this GetChildren script to work ( Advance? )

So In my game you can collect stars, the stars are located in a model that is spawned in the workspace from replicated storage. Stars are local and there are about 20 stars in the whole game.

This is the script that gives the player the 1 point for collecting a star. It gets the children of the model and if the player touches the Part he gets a point. It works fine until I try to make the stars spin around. It only spins 1 star and all the other stars stay still.

local Stars = workspace.Stars:GetChildren()
local player = game.Players.LocalPlayer

for i, v in pairs(Stars) do
    if v.Name == "Star" then
        v.Touched:Connect(function(hit)
     if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
            if hit.Parent.Name == player.Name then
            game.ReplicatedStorage.StarsEvent:Fire()
            v:Destroy()
               end
           end
       end)
    end
while true do  
	v.CFrame = v.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 5)  
	wait()  -- This part only spins 1 star not all 20 of them.
end
end

1 comment

0

Thats because when it gets to 1 star it makes a while true do and then never gets out of it.

1 answer

✓ Accepted answer

It only gets to edit one star because there's an infinite loop inside another loop. Although, this can be fixed by wrapping it in a coroutine or using spawn.

Here's a solution I made for you:

coroutine.wrap(function()
    while true do  
        v.CFrame = v.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 5)  
        wait()  -- This part only spins 1 star not all 20 of them.
    end
end)()
end

This solution works fine, but can get heavy with so many threads on the server It'd be a better solution if you combined everything into only 1 loop. This wouldn't need a coroutine, unless there was code after that.

local Stars = workspace.Stars:GetChildren()
local player = game.Players.LocalPlayer

for i, v in pairs(Stars) do
    if v.Name == "Star" then
        v.Touched:Connect(function(hit)
     if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
            if hit.Parent.Name == player.Name then
            game.ReplicatedStorage.StarsEvent:Fire()
            v:Destroy()
               end
           end
       end)
    end
end
while true do  
    for i, v in pairs(Stars) do
        v.CFrame = v.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 5)  
        wait()  -- This part only spins 1 star not all 20 of them.
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.