So, I'm trying to make a brick whom will give admin commands to people who touch it. I have this so far:
Admin = {} script.Parent.Touched:connect(function(hit) if hit.Parent:findFirstChild("Humanoid") then Admin[hit.Parent.Name]=game.Players:GetPlayerFromCharacter(hit.Parent) end end) for i=1,#Admin do if game.Players:FindFirstChild(Admin[i]) ~= nil then A=game.Players:FindFirstChild(Admin[i]) end end A.Chatted:connect(function(msg) if string.sub(msg,1,5) == "give;" then p = Game:GetService("InsertService"):loadAsset(string.sub(msg,6)) p.Parent = Game.Workspace p:MakeJoints() p:MoveTo(game.Workspace.Admin[i].Torso.Position) end end)
Output: 21:48:02.545 - Workspace.Acquire "give;" Commands.Head.Script:14: attempt to index global 'A' (a nil value)
So, I'm wondering, how do you make it work and not break since the Admins list is empty at the first but the script runs immediately and breaks.. Any clues how to fix it?
Thanks!
Make a separate Chatted event for the people that touch the part and make lines 15 through 20 a function.
Admin = {} script.Parent.Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player and not Admin[player.Name] then Admin[player.Name] = player player.Chatted:connect(function(msg) if string.sub(msg,1,5) == "give;" and player.Character then p = Game:GetService("InsertService"):loadAsset(string.sub(msg,6)) p.Parent = Game.Workspace p:MakeJoints() p:MoveTo(player.Character.Torso.Position) end end) end end)
Edit: Sorry, I thought you wanted names inside the table already to work.
A working and more efficient way of doing this is connecting the Chatted event inside the Touched event.
Admin = {} script.Parent.Touched:connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then -- using GetPlayerFromCharacter will detect if it's a player that touched the brick, not just anything with a Humanoid in it. local player = game.Players:GetPlayerFromCharacter(hit.Parent) if not pcall(function() return Admin[player.Name] end) then Admin[player.Name] = player player.Chatted:connect(function(msg) if string.sub(msg,1,5) == "give;" then p = Game:GetService("InsertService"):LoadAsset(string.sub(msg,6)) p.Parent = Game.Workspace p:MakeJoints() p:MoveTo(player.Character.Torso.Position) end end) end end end)
Locked by JesseSong
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?