Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

Help with CFrame Loops?

Asked by 10 years ago

Well I have a script that gets the children from a "Dummys" model, yes I know it should be spell "dummies" this is the internet, well past that bit, it should "spawn" a "dummy" at every "spawn" but no at the same one, that bit works, it just stops around 4 "dummys" left no error or anything.

01wait(3)
02for _,v in pairs (game.Workspace.Dummys:GetChildren()) do
03    wait(0.3)
04    local RandomSpawn=game.Workspace.Spawns:children''[math.random(1,#game.Workspace.Spawns:children'')]
05        if RandomSpawn:FindFirstChild("Used") then
06        if not RandomSpawn:FindFirstChild("Used").Value then
07            RandomSpawn:findFirstChild("Used").Value = true
08            v.Torso.CFrame = CFrame.new(RandomSpawn.Position) + Vector3.new(0, 1, 0)
09            wait()
10        end
11    end
12end

Is my main script and only spawns is like

Game/Workspace/Spawns

"Dummys" are Game/Workspace/Dummys ----Gif of it

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Here's a cleaned up version of what you wrote (better names, spacing, style) (using :children'' is absurd) that does exactly what you did:

01wait(3)
02local spawns = game.Workspace.Spawns:GetChildren()
03 
04for _, dummy in pairs (game.Workspace.Dummys:GetChildren()) do
05    wait(0.3)
06    local RandomSpawn = spawns[math.random(1, #spawns)]
07    if RandomSpawn:FindFirstChild("Used") then
08        if not RandomSpawn.Used.Value then
09            RandomSpawn.Used.Value = true
10            dummy.Torso.CFrame = CFrame.new(RandomSpawn.Position)
11                + Vector3.new(0, 1, 0)
12        end
13    end
14    wait()
15end

Remark What happens when the spawn you picked randomly has already been picked? You don't move the dummy; the dummy remains where it is, and you move onto the next one.

You instead ought to continue trying until you manage to place it. A repeat until loop is a brief way to accomplish that.

01wait(3)
02local spawns = game.Workspace.Spawns:GetChildren()
03for _, dummy in pairs (game.Workspace.Dummys:GetChildren()) do
04    wait(0.3)
05    local RandomSpawn -- Declare RandomSpawn's scope.
06    repeat
07        -- Maybe this one will work?
08        RandomSpawn = spawns[math.random(1, #spawns)]
09        wait() -- Just in case you run out, instead of crashing,
10        -- it will just stop placing.
11    until RandomSpawn:FindFirstChild("Used") and not RandomSpawn.UsedValue
12    -- RandomSpawn has not yet been used, so use it now
13    RandomSpawn.Used.Value = true
14    dummy.Torso.CFrame = CFrame.new(RandomSpawn.Position)
15        + Vector3.new(0, 1, 0)
16end

Alternatively, instead of using Used, you could just remove random objects from your list of spawns, which is much more straightforward:

01wait(3)
02local spawns = game.Workspace.Spawns:GetChildren()
03 
04for _, dummy in pairs (game.Workspace.Dummys:GetChildren()) do
05    wait(0.3)
06    local RandomSpawn = table.remove(spawns, math.random(1,# spawns))
07    -- RandomSpawn has not yet been used -- and is now gone from the list
08    -- for future dummies
09    dummy.Torso.CFrame = CFrame.new(RandomSpawn.Position)
10        + Vector3.new(0, 1, 0)
11end
0
Thank you so much!! harvest109 0 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

I don't think ":children" is an actual method. I pointed this out in my answer to your forum post and you said it worked, so moving on...

The problem is that if you find a spawn that's used you never try and look for another spawn. You just do nothing. This means that if you toggle one spawn as "Used" and the script randomly picks that spawn, it won't spawn the dummy anywhere.

You can fix that by repeating that by repicking the RandomSpawn until it's not used:

1repeat
2local RandomSpawn=game.Workspace.Spawns:children''[math.random(1,#game.Workspace.Spawns:children'')]
3until not RandomSpawn:FindFirstChild("Used").Value
4--rest of the code
0
:children is a method. It's equivalent to GetChildren, except that it's deprecated. BlueTaslem 18071 — 10y

Answer this question