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

Help with this script? Teleportation to specific bricks.

Asked by 8 years ago

Hello, I asked this earlier, and I do need a bit more help

local SP1 = game.Workspace.SpawnPs["SP-1"]
local SP2 = game.Workspace.SpawnPs["SP-2"]
local SP3 = game.Workspace.SpawnPs["SP-3"]
local SP4 = game.Workspace.SpawnPs["SP-4"]
local SP5 = game.Workspace.SpawnPs["SP-5"]
local SP6 = game.Workspace.SpawnPs["SP-6"]
local SP7 = game.Workspace.SpawnPs["SP-7"]
local SP8 = game.Workspace.SpawnPs["SP-8"]
local SP9 = game.Workspace.SpawnPs["SP-9"]
local SP10 = game.Workspace.SpawnPs["SP-10"]

local players = game.Players
local spawnParts = SP1,SP2,SP4,SP5,SP6,SP7,SP8,SP9,SP10

    game.Workspace.GameStats.GameRunning.Changed:Connect(function() -- you said fire on workspace value changed.
    if game.Workspace.Value.Value == true then
        for i,v in pairs(game.Players:GetPlayers()) do -- Loop through all players
            if v.Character then
                if v.Character:FindFirstChild("Torso") then
                    v.Character.Torso.CFrame = spawnParts["SP-"..math.random(1,10)] -- Set the torso's cframe to spParts.SP-(1 to 10)
                end
            end
        end
    end
end)


For some reason, its not bringing players to the parts. Any help appreciated!

0
You aren't getting your spawn part's position(s) Warfaresh0t 414 — 8y
0
Aye, how would I get that? Would it be something with like brick.Position QuantumWaffle 17 — 8y
0
Yep Warfaresh0t 414 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

For one, you don't need to define all of those spawn positions. You can simply just call GetChildren on SpawnPs, which will return an array of all the instances that you can just randomly index.

Your actual problem is you're trying to assign a tuple to spawnParts with a single variable, which is a logical fallacy (a single variable can't have more than one reference at the same time). You probably meant to put that tuple inside of a table, which is still redundant since like I said before, you can just use GetChildren. Here, try this:

local players = game:GetService("Players")
local spawnParts = workspace:WaitForChild("SpawnPs")
local spawns = spawnParts:GetChildren()

ValueObject.Changed:Connect(function()
    if (Condition) then
        for _, player in next, players:GetPlayers() do
            local torso = player.Character and player.Character:FindFirstChild("Torso")
            if torso then
                -- Get a random number between 1-#spawns, and return the object in that index. This is the standard method of returning pseudo-random data from an array.
                local randomCF = spawns[math.random(#spawns)].CFrame
                torso.CFrame = randomCF
            end
        end
    end
end)

If you have any questions, just let me know.

Ad
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

Since i answered your previous answer, i should fix my own mistake(s) XD

--line 1-10, 13
spawnParts = game.Workspace.SpawnPs:GetChildren()

--line 16
--Probably change that to 
if game.Workspace.GameStats.GameRunning.Value == true then

--line 20
v.Character.Torso.CFrame = spawnParts["SP-"..math.random(1,#spawnParts)].CFrame + Vector3.new(0,5,0)

0
Why index it with a key concatenated with a random index? Also, you should use `#` to get the length of the table for the random number range, since it has the potential to vary. After that, you can simply just index the array as: t[math.random(#t)] ScriptGuider 5640 — 8y
0
to make it simpler to understand for him. hes new. RubenKan 3615 — 8y
1
But that's even more complicated, lol. The concatenation is unnecessary, you should always encourage good practice in an early state of learning. Being confused is a good sign, and it's something to pay attention to. It's your brain telling you there's more to learn, and that's not something to neglect. ScriptGuider 5640 — 8y

Answer this question