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

How do I make a script run when someone resets their character?

Asked by 6 years ago

I have a script in my server script service that gives someone a gun if they have a certain gamepass but it only gives it to them when they join. When the person resets or dies, they lose the weapon. How do I get the script to run again after this?

2 answers

Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
6 years ago

I would recommend using the .Died event as Bucwheed suggested.

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local hum = player.Character:WaitForChild("Humanoid")

hum.Died:connect(function()
    -- insert whatever you want in here
end)
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
6 years ago

Use characteradded.. something like this.

giveGun = function()
    print('Hi!')
end
game.Players.PlayerAdded:connect(function(p)
    function a(player, id) --So you don't have to redefine a variable all the time
        return game:GetService("GamePassService"):PlayerHasPass(player, id) -- checks if the player has your gamepass
    end
    p.CharacterAdded:connect(function(c) -- this is the part that you needed help with. everytime the character spawns, it continues to the next code block
        if a(p, 364868976) then --no, i just grabbed a id from a random gamepass I looked at
            giveGun() --gives you the gun
        else 
            print('no wep for u :(')
        end
    end)
end)

Humanoid.Died probably wouldn't work because everytime you die, the localscript in StarterPack reloads. Therefore, completely ruining the purpose of using .Died in the first place.

BUT, .Died can also be used to make you drop all weapons that you had on death. Oh, yeah. If you use p.Character:FindFirstChildOfClass("Humanoid").Died in a serverscript, that could work too.. probably.

if I missed something, tell me instead of flaming me. Thanks.

Answer this question