0

How to limit the script to check if Table is not empty and run it each time it's changed? [closed]

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
end
end)

end
end
A.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "give;"  then
p.Parent = Game.Workspace
p:MakeJoints()
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!

Locked by JesseSong

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

1
nate890 495
9 years ago

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
player.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "give;" and player.Character  then
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.

0
Your's worked but after I fixed what I had to fix.. CrniOrao 28 — 9y
1
Axstin 55
9 years ago

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

player.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "give;" then
p.Parent = Game.Workspace
p:MakeJoints()
p:MoveTo(player.Character.Torso.Position)
end
end)
end
end
end)

0
This works fine. But the problem is I keep getting multiple entires of my name into the Table so it reproduces multiple items that were generated by using the command. CrniOrao 28 — 9y
0
Oh, yea. I forgot to make it check if the player is already in the Admin table before it adds the player, fixed it. Axstin 55 — 9y
0
Okay, thank you very much! :) CrniOrao 28 — 9y
0
For some reason it doesn't even add players into the Admin section. CrniOrao 28 — 9y