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

How do I make a function keep working even after a player has died?

Asked by
Hasburo 150
8 years ago
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.

2 answers

Log in to vote
3
Answered by 8 years ago

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().

  • Why didn't you script work?

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!

0
It helped greatly! However, sometimes it doesn't give the specified player the uniform after dying unless you do the command several other times. Any way to counter/fix this? Hasburo 150 — 8y
0
You can try making a variable of the player name to make sure that the added instance is that player that has the effects of the command. Your table may be changing for the [i] which causes it to wait for another person. Can't tell much w/o full script. alphawolvess 1784 — 8y
Ad
Log in to vote
0
Answered by
xuefei123 214 Moderation Voter
8 years ago

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.

0
I don't see how your example used the .Changed function? Perhaps I'm just not seeing/understanding it. Hasburo 150 — 8y
0
Oh, sorry, typo. Please accept the answer anyway :) xuefei123 214 — 8y
0
I see. But this doesn't really help me though considering the script posted is command based, rather than when a player enters the server. Hasburo 150 — 8y
0
Have a look now xuefei123 214 — 8y
View all comments (4 more)
0
Perhaps I should've explained the situation better. This code is in an custom admin script, and this is a command in the admin script. I cannot use PlayerAdded because it gives a uniform to whoever is specified in the command, not whoever joins the server. For example, :uniform me or :uniform player. Hasburo 150 — 8y
0
But I helped, did I not? So if I did, please accept the answer xuefei123 214 — 8y
0
You did not help, unfortunately. Although I truly do appreciate the effort, it still leaves my question unanswered. Hasburo 150 — 8y
0
... Check again. You will have to edit the script, but I have the clothing part in it and the main concept xuefei123 214 — 8y

Answer this question