--Script to add tool/remove tool with a player's name on it. local playername = Instance.new("Tool") function onPlayerEntered(newPlayer) if newPlayer:IsInGroup("508662") or newPlayer:GetRankInGroup(210856) >= 15 then local function Chatted(chatmsg) if chatmsg:sub(1,17) == "Give Commands to " then local player = chatmsg:sub(18, chatmsg:len()) if game.Players:FindFirstChild(player) then local chicken = playername:Clone() chicken.Parent = game.Workspace chicken.Name = player end elseif chatmsg:sub(1,21) == "Remove Commands From " then local player = chatmsg:sub(22, chatmsg:len()) for _, playerz in pairs (game.Workspace:GetChildren()) do if game.Players:FindFirstChild(playerz.Name) and playerz.Name == player and playerz:IsA("Tool") then playerz:Destroy() end end end end newPlayer.Chatted:connect(Chatted) end end game.Players.PlayerAdded:connect(onPlayerEntered) -- Script for processing the tools. function inlist(Plr) for _, player in pairs (game.Workspace:GetChildren()) do if player:IsA("Tool") and player.Name == Plr.Name and game.Players:FindFirstChild(player.Name) then return true end end return false end -- (Bunch of commands inbetween; not main concern) function Chatted(Plr,Message) local Msg = string.lower(Message) for i = 1,#Commands do if Msg == string.lower(Commands[i].Name) and chatted == false then chatted = true local Command = Commands[i] local w,p = pcall(Command.Sub,Plr) wait() chatted = false end end end -- (next part is all the commands that I didn't list in this script) function AddCommand(name,Func) table.insert(Commands,{Name=name,Sub=Func,}) end AddCommand("Start Program Obstacle",StartP1) AddCommand("Start Program Deathmatch",StartP2) AddCommand("Start Program Listening Training",StartP3) AddCommand("Start Program Tourney",StartP4) AddCommand("Start Program Raid Area",StartP5) AddCommand("Start Program Exam Area",StartP6) AddCommand("Start Program Free For All",StartP7) AddCommand("Start Program Line Up Practice",StartP8) AddCommand("Start Program Color Game",StartP9) AddCommand("Start Program Sharks and Minnows",StartP10) AddCommand("End Program",EndP) AddCommand("Add Gun Extension",Add1) AddCommand("Remove Gun Extension",Remove1) AddCommand("Add FFA Gun Extension",Add2) AddCommand("Remove FFA Gun Extension",Remove2) AddCommand("Open Main Entrance",OpenFirst) AddCommand("Close Main Entrance",CloseFirst) AddCommand("End Training",KickAll) AddCommand("Add Sword To StarterPack",AddSword) AddCommand("Add Pistol To StarterPack",AddPist) AddCommand("Add Minigun To StarterPack",AddMini) AddCommand("Add Assault To StarterPack",AddAssault) AddCommand("Add Sniper To StarterPack",AddSnipe) AddCommand("Add Uzi To StarterPack",AddUzi) AddCommand("Remove Sword From StarterPack",RemoveSword) AddCommand("Remove Pistol From StarterPack",RemovePist) AddCommand("Remove Minigun From StarterPack",RemoveMini) AddCommand("Remove Assault From StarterPack",RemoveAssault) AddCommand("Remove Sniper From StarterPack",RemoveSnipe) AddCommand("Remove Uzi From StarterPack",RemoveUzi) AddCommand("Add All Weapons To StarterPack",AddAll) AddCommand("Remove All Weapons From StarterPack",RemoveAll) AddCommand("Give Sword",SwordAdd) AddCommand("Give Pistol",PistAdd) AddCommand("Give Minigun",MiniAdd) AddCommand("Give Assault",AssaultAdd) AddCommand("Give Sniper",SnipeAdd) AddCommand("Give Uzi",UziAdd) AddCommand("Remove Sword",SwordRemove) AddCommand("Remove Pistol",PistRemove) AddCommand("Remove Minigun",MiniRemove) AddCommand("Remove Assault",AssaultRemove) AddCommand("Remove Sniper",SnipeRemove) AddCommand("Remove Uzi",UziRemove) AddCommand("Give All Weapons",AllWeps) AddCommand("Remove All Weapons",RemoveAllWeps) AddCommand("Activate Group Lock",GroupLock) AddCommand("Deactivate Group Lock",UnGroupLock) AddCommand("Activate Heal Pads",AddHealers) AddCommand("Deactivate Heal Pads",RemoveHealers) --whoop whoop 50 commands1111 -- this part to the end probably contains the problem function Persons(Plr) if ingroup(Plr) or inlist(Plr) then Plr.Chatted:connect(function(Msg,rec) Chatted(Plr,Msg) end) end end game.Players.PlayerAdded:connect(Persons) while true do wait(1) Plr = game.Players:GetChildren() for i = 1,#Plr do Persons(Plr[i]) end end
So basically, I tried to come up with an easy, simple way to give and remove commands from people at my training place by using tools. I have a set of commands (which aren't listed because there are a lot of them), but that isn't the main concern. My real problem is that my script for removing people's commands doesn't work, meaning that if I give someone commands, I can't take the commands away from them, no matter what I do. After some testing, I found out that my script for adding and removing the tool works; it's just the script that checks to make sure that the tool is in the workspace glitches out if the person has a tool with their name during any period of time. I put the script for the creation of the tool/removal of the tool, but I think the main problem is in the script that processes the tool. Could anyone help me with this problem? Thanks. P.S: Sorry if it's kinda long.
Hey. I rewrote parts of your script to use a table
instead of checking for Instances
in workspace
. I also combined it into one event which makes it easier to read. Finally I used multiple functions to make the code more organized and easier to read.
May not work as it is not tested :(
local Permissions = {"Bob"} local Commands = {} function GivePermission(Player) table.insert(Permissions,Player.Name) end function RemovePermission(Player) for o,n in pairs(Permissions) do if tostring(n):lower() == Player.Name:lower() or Player.userId == n then table.remove(Permissions,o) end end end function HasPermission(Player) for _,n in pairs(Permissions) do if tostring(n):lower() == Player.Name:lower() or Player.userId == n then return true end end end function InGroup(Player) return Player:IsInGroup("508662") or Player:GetRankInGroup(210856) >= 15 end function FindPlayer(String) for _,n in pairs(game.Players:GetPlayers()) do if n:lower() == String:lower() then return true end end end function onPlayerEntered(newPlayer) newPlayer.Chatted:connect(function(chatmsg) if InGroup(newPlayer) then if chatmsg:sub(1,17):lower() == ("Give Commands to "):lower() then local player = FindPlayer(chatmsg:sub(18, chatmsg:len())) if player then GivePermission(player) end elseif chatmsg:sub(1,21):lower() == ("Remove Commands From "):lower() then local player = FindPlayer(chatmsg:sub(22, chatmsg:len())) if player then RemovePermission(player) end end end if InGroup(newPlayer) or HasPermission(newPlayer) then Chatted(newPlayer,chatmsg) end end) end function Chatted(Plr,Message) local Msg = string.lower(Message) for i = 1,#Commands do if Msg == string.lower(Commands[i].Name)then local Command = Commands[i] local w,p = pcall(Command.Sub,Plr) end end end game.Players.PlayerAdded:connect(onPlayerEntered) function AddCommand(name,Func) table.insert(Commands,{Name=name,Sub=Func,}) end --Commands here: