if playerQuery[i].Character and playerQuery[i].Character:FindFirstChild("Torso") and playerQuery[i].Character:FindFirstChild("Humanoid") then function uniform() playerQuery[i].Character.Shirt:Destroy() playerQuery[i].Character.Pants:Destroy() local shirt = Instance.new("Shirt", playerQuery[i].Character) local pants = Instance.new("Pants", playerQuery[i].Character) shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=281088169" pants.PantsTemplate = "http://www.roblox.com/asset/?id=281088561" end if (playerQuery[i].Character:findFirstChild("Humanoid")) then playerQuery[i].Character.Humanoid.Died:connect(function() uniform() end) end end end end
playerQuery is already defined as the player, by the way. Also, there is more to this script but it is a lot of coding so I only posted the lines necessary.
The purpose of this script is to give a uniform to a player on command, which works but I'd like to give the uniform to the player permanently.. meaning I'd like the player to have the uniform even after the player dies/respawns.
Please note that I am making assumptions on the conditions and meanings of things inside of your code for you have only provided, as said, a section of the problematic code. I am assuming playerQuery[i] is a table containing Player Instances, i being the player that the admin is currently focusing on with the command.
** I have changed the tabbing of your code that you'll need to correct when inserting it into the main script, this is so I could have edited faster and read it easier to help **
Look bellow for the hopefully fixed segment of code, I have comments explaining certain things.
if playerQuery[i].Character and playerQuery[i].Character:FindFirstChild("Torso") and playerQuery[i].Character:FindFirstChild("Humanoid") then function uniform() playerQuery[i].Character.Shirt:Destroy() playerQuery[i].Character.Pants:Destroy() local shirt = Instance.new("Shirt", playerQuery[i].Character) local pants = Instance.new("Pants", playerQuery[i].Character) shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=281088169" pants.PantsTemplate = "http://www.roblox.com/asset/?id=281088561" end if (playerQuery[i].Character:findFirstChild("Humanoid")) then uniform() -- Put the uniform on the character first local Added = game.Workspace.ChildAdded:connect(function(Char) -- Fires every player respawn, the Char gets added again after death if Char.Name == playerQuery[i].Name then -- Make sure it is the correct person; assuming this works uniform() end end) end end
You'll notice that I made the event ChildAdded
become a Variable. You can use this variable, if you change the scope, to disconnect the event if you want a command to stop giving uniform on respawn. This would be done by Added:disconnect()
.
Assuming the table works the way you want it, you're firing the Died event then giving their uniform. So the second they die, they get a uniform BEFORE they respawn, causing their new body to not be updated with the uniform.
Comment any questions, accept this answer if it helped!
For this script, you should use the .CharacterAdded() function, here is an example: Without the command
game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char)--char is the players character char.Shirt:Destroy() char.Pants:Destroy() local shirt = Instance.new("Shirt", char) local pants = Instance.new("Pants", char) shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=281088169" pants.PantsTemplate = "http://www.roblox.com/asset/?id=281088561" end) end)
Here is the script for a .Chatted command:
local change = false game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char)--char is the players character if change then char.Shirt:Destroy() char.Pants:Destroy() local shirt = Instance.new("Shirt", char) local pants = Instance.new("Pants", char) shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=281088169" pants.PantsTemplate = "http://www.roblox.com/asset/?id=281088561" end end) end) game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) if msg == "cmd" then change = true end end)
Here is the script for only admins:
local admins = {}--Add admins local change = false game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char)--char is the players character if change then char.Shirt:Destroy() char.Pants:Destroy() local shirt = Instance.new("Shirt", char) local pants = Instance.new("Pants", char) shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=281088169" pants.PantsTemplate = "http://www.roblox.com/asset/?id=281088561" end end) end) game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) if msg == "cmd" and admins[p.Name] then change = true end end)
If this helps, accept the answer, it gives us both rep
Also, .CharacterAdded only gets fired when a character gets spawned in, eg after :LoadCharacter() is fired and if the humanoid is killed.