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

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

Asked by 10 years ago

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!

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?

2 answers

Log in to vote
1
Answered by
nate890 495 Moderation Voter
10 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
        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.

0
Your's worked but after I fixed what I had to fix.. CrniOrao 28 — 10y
Ad
Log in to vote
1
Answered by
Axstin 55
10 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
            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)
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 — 10y
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 — 10y
0
Okay, thank you very much! :) CrniOrao 28 — 10y
0
For some reason it doesn't even add players into the Admin section. CrniOrao 28 — 10y