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

Script Doesn't Detect Global Table?

Asked by 5 years ago
Edited 5 years ago

I was making two scripts, one that detects if a player is touching a part and if so adds them to a table, and the other is a round script that is supposed to teleport the players in the table but for some reason the round script doesn't detect the table? Here are the scripts (Both are server scripts, round script is in workspace and the part detection script is in a part which is also located in workspace)(Here is the script that detects the players touching the part):

PlayersTouching = {}
local currentlytouching = true

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    currentlytouching = true
        local plr = workspace[hit.Parent.Name] --Access character from workspace.
        local player = game.Players:GetPlayerFromCharacter(plr) --Access (plr)
        local playernumber = #PlayersTouching + 1
        table.insert(PlayersTouching, ""..playernumber.."", ""..plr.."")
        if PlayersTouching[""..playernumber..""] == ""..plr.."" then
            print(""..plr.."")
wait until currentlytouching == false
        end
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    currentlytouching = false
end)

Here is the round script:

    local Pos
    local currentnum = 1
while wait() do
    game.ServerStorage.ElevatorValues.Easy.Intermission.Value = 65
        for i = 1, 65 do
            wait(1)
            game.ServerStorage.ElevatorValues.Easy.Intermission.Value = game.ServerStorage.ElevatorValues.Easy.Intermission.Value - 1
        end
    wait(1)
    map = game.Lighting.EasyMaps:GetChildren()
    mathrandom = math.random(1,#map)
    map = map[mathrandom]:clone()
    map.Parent = workspace
    Pos = map:findFirstChild("Spawn")
    for i = 1, #PlayersTouching do
        game.Workspace:findFirstChild(""..PlayersTouching.currentnum..""):moveTo(Pos.Position) wait()

    end
    wait(5)
    game.ServerStorage.ElevatorValues.Easy.EstimatedTime.Value = 30
    repeat
        wait(1)
        game.ServerStorage.ElevatorValues.Easy.EstimatedTime.Value = game.ServerStorage.ElevatorValues.Easy.EstimatedTime.Value - 1
    until game.ServerStorage.ElevatorValues.Easy.EstimatedTime.Value == 0 or game.ServerStorage.ElevatorValues.Easy.PlayersRemaining.Value == 0
    map:remove()
end

(The Round Script Spawns In The Map But Doesn't Teleport The Player And I Get This Error): Workspace.EasyRoundScript:15: attempt to get length of global 'PlayersTouching' (a nil value) If you have any idea why this isn't working please let me know!

1
Your `PlayersTouching` variable is global, yes, but will not be seen by other scripts. You should never be using global variables anyway. If you find a need for them, your code needs to be restructured. You can instead use a module script. User#24403 69 — 5y
0
Since the PlayersTouching Table is being read as nil, this means that is still believes there are no values, as incapaxx said, its global within the script, so try combining the scripts together by placing your while loop at the end of the first script SerpentineKing 3885 — 5y
0
You could use _G, but you'll get yelled at. DinozCreates 1070 — 5y
0
Combining Scripts Togethor By Placing While Loop At The End Of The First Script? What Do You Mean By That? kizi3000 88 — 5y
View all comments (4 more)
0
So put the second script's coding into the first script. I would put the second script's lines of code below the first function since having a loop above the function may make you unable to call it SerpentineKing 3885 — 5y
0
like this: currentlytouching = false end) local Pos ... etc. (also now that i wrote that, you may need to define Pos) SerpentineKing 3885 — 5y
0
Pos doesn't mean anything yet, it is supposed to be the position of the part that the players will teleport to kizi3000 88 — 5y
0
I still don't understand how to combine the scripts because when I tried to do that it doesn't do any part of the round script kizi3000 88 — 5y

Answer this question