Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

2 answers

✓ Accepted answer

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.

1 comment

0

Your's worked but after I fixed what I had to fix..

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)

4 comments

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.

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.

0

Okay, thank you very much! :)

0

For some reason it doesn't even add players into the Admin section.

This discussion is preserved as it was. New answers, comments, and votes are closed.