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

Can someone help me with this script?

Asked by 9 years ago

Alright, so I'm trying to make a script where someone touches a brick. The first person to touch it becomes the owner. Then any further users who touch it will cause a GUI to send to the owner asking if they can join their team. If the owner presses yes, it will teleport the user in, if not, then nothing will happen. Here are the scripts:

The first script:

locked = false
tpTo = Vector3.new(0, 20, 0)
owner = ""
_G.names = {""}

script.Parent.Touched:connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        local name = part.Parent.Name

        if locked == false then
            owner = name
            locked = true
            part.Parent:MoveTo(tpTo) -- TELEPORT
            print("Owner touched the brick")
        else if locked == true and name ~= owner then
            table.insert(_G.names, name)
            game.Players[owner].PlayerGui.AskGui.Frame.Visible = true
            game.Players[owner].PlayerGui.AskGui.User.Text = name.." is asking to join your team. Accept this player?"
        end

    end
    end
    end)

And here is the localscript in the "Yes" button that the owner can press to accept a user:

tpTo = Vector3.new(0, 20, 0)
function tp()
game.Players[_G.names[#_G.names]].Character:MoveTo(tpTo)
script.Parent.Parent.Visible = false
end
script.Parent.MouseButton1Down:connect(tp)

I keep getting an error saying: "Players.Player1.PlayerGui.AskGui.Frame.Yes.LocalScript:3: attempt to get length of field 'names' (a nil value)"

1 answer

Log in to vote
1
Answered by 9 years ago

You need to use a for loop to cycle through the players, your treating the players like its a single object, but its in reality an array or table of objects, example below.

for i = 1, #_G.names do
    game.Players:FindFirstChild( _G.names[i]).Character:MoveTo(tpTo)
end

This should work, mark correct if so, but if its wrong please let me know. I may have messed up global variables a bit so let me know if so, but this should be the basic concept you would want to use for line three in the local script named Yes.

0
I think that is working, but this part isn't working: game.Players[owner].PlayerGui.AskGui.Frame.Visible = true 18 game.Players[owner].PlayerGui.AskGui.User.Text = name.." is asking to join your team. Accept this player?" The GUI isn't showing up on the owner's screen when someone touches the brick. Any ideas? intrance 50 — 9y
0
try doing print(game.Players:FindFirstChild(owner)) before that line, make sure you are getting the owner referenced correctly dragonkeeper467 453 — 9y
0
Okay that helped a lot. I realized that once it ran once, the script stopped responding. intrance 50 — 9y
0
It never gets to the second if statement. intrance 50 — 9y
View all comments (2 more)
0
Nevermind. It works now! Thank you so much! intrance 50 — 9y
0
Np dragonkeeper467 453 — 9y
Ad

Answer this question