How do I reset a serverscript when someone dies

I have a system where at a certain playtime you get a gun
but when you die your guns go away.
so how do I make it that the guns come back

Script

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local timeplayed = Instance.new(“IntValue”)
timeplayed.Name = “PlayTime”
timeplayed.Parent = leaderstats

while true do
	wait(1)
	timeplayed.Value += 1

	if timeplayed.Value == 50 then
		local M4A1 = game.ReplicatedStorage.M4A1
		local M4clone = M4A1:Clone()
		M4clone.Parent = player.Backpack
	elseif timeplayed.Value == 100 then
		local G36C = game.ReplicatedStorage.G36C
		local G36Cclone = G36C:Clone()
		G36C.Parent = player.Backpack
	elseif timeplayed.Value == 200 then
		local HK416 = game.ReplicatedStorage.HK416
		local HK416Cclone = HK416:Clone()
		HK416.Parent = player.Backpack
	elseif timeplayed.Value == 300 then
		local RPK = game.ReplicatedStorage.RPK
		local RPKCclone = RPK:Clone()
		RPK.Parent = player.Backpack
	elseif timeplayed.Value == 400 then
		local UMP45 = game.ReplicatedStorage.UMP45
		local UMP45Cclone = UMP45:Clone()
		UMP45.Parent = player.Backpack


	end
end

end)

2 Likes

you could run a check if the player died or humanoid health is 0 and then destroy their inventory

1 Like

But im trying to make the guns come back when you respawn

Huh but they die doesn’t that mean their guns are gone already?

yeah but the script is supposed to get the value of time played and get the guns
but once you die it doesnt do that anymore

I understood nothing from it, or what you want with the script

so basically the script makes a leaderstat called Play time,
and when it hits a certain time it gives you a gun
but when you respawn it doesnt work anymore.

Why don’t you just have an it statement inside the loop that increases the playtime that checks if each players playtime is greater than the amount needed for a gun? If this is true, check if the player already has the gun, if they don’t, give it to them.

All you need the change is the order in which you check the numbers (greatest to least), then check if their stat is greater than the number, not equal to it.

2 Likes

how do i check if its greater?

You use operators just like greater than less than or greater than equal to

1 Like

if value >= number then
Read the post above.

so like this?
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local timeplayed = Instance.new(“IntValue”)
timeplayed.Name = “PlayTime”
timeplayed.Parent = leaderstats

while true do
	wait(1)
	timeplayed.Value += 1

	if timeplayed.Value == 400 then
local UMP45 = game.ReplicatedStorage.UMP45
local UMP45Cclone = UMP45:Clone()
UMP45.Parent = player.Backpack
	elseif PlayTime <= 400 then
      if  timeplayed.Value == 300 then 
		local RPK = game.ReplicatedStorage.RPK
		local RPKCclone = RPK:Clone()
		RPK.Parent = player.Backpack
	elseif PlayTime <= 300 then
       if timeplayed.Value == 200 then
		local HK416 = game.ReplicatedStorage.HK416
		local HK416Cclone = HK416:Clone()
		HK416.Parent = player.Backpack
	elseif PlayTime <= 200 then
        if timeplayed.Value == 100 then
		local G36C = game.ReplicatedStorage.G36C
		local G36Cclone = G36C:Clone()
		G36C.Parent = player.Backpack
		elseif Playtime <= 100 then
       if timeplayed.Value == 50 then
			local M4A1 = game.ReplicatedStorage.M4A1
			local M4clone = M4A1:Clone()
			M4clone.Parent = player.Backpack

I’m a little confused what PlayTime is, since you already have timeplayed. But otherwise the concept looks somewhat better, since some lines still have ==. Think of it this way, players are only going to have the exact playtime for one second, you need to give them the weapon if they have anything more than that time.

this is as far as i got i dont really know what to do
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local timeplayed = Instance.new(“IntValue”)
timeplayed.Name = “Play Time”
timeplayed.Parent = leaderstats

		while true do
			wait(1)
			timeplayed.Value += 1

			if timeplayed.Value == 50 then
				local M4A1 = game.ReplicatedStorage.M4A1
				local M4clone = M4A1:Clone()
				M4clone.Parent = player.Backpack
			elseif timeplayed >= 50 then
               if timeplayed.Value == 100 then
				local G36C = game.ReplicatedStorage.G36C
				local G36Cclone = G36C:Clone()
				G36C.Parent = player.Backpack
				elseif timeplayed >= 100 then
              if timeplayed.Value == 200 then
				local HK416 = game.ReplicatedStorage.HK416
				local HK416Cclone = HK416:Clone()
				HK416.Parent = player.Backpack
			elseif timeplayed.Value == 300 then
				local RPK = game.ReplicatedStorage.RPK
				local RPKCclone = RPK:Clone()
				RPK.Parent = player.Backpack
				elseif timeplayed >= 300 then
              if  timeplayed.Value == 400 then
				local UMP45 = game.ReplicatedStorage.UMP45
				local UMP45Cclone = UMP45:Clone()
				UMP45.Parent = player.Backpack

end
end
end
end
end
end)
doesnt work

Well first off you un-reversed the order, so that will give you multiple items as you go on. (Not sure how you want it.) But importantly, you have random if and elseif statements inside each other that make 0 logical sense. You should only have one condition relating to timeplayed for each required value.

Since you said you don’t know what to do, I suggest you read up on these here and here or on another tutorial.

i just realised i was such an idiot lol

this is what i got now

game.Players.PlayerAdded:Connect(function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
		local timeplayed = Instance.new("IntValue")
		timeplayed.Name = "Play Time"
		timeplayed.Parent = leaderstats

		while true do
			wait(1)
			timeplayed.Value += 1

			if timeplayed.Value == 50 then
				local M4A1 = game.ReplicatedStorage.M4A1
				local M4clone = M4A1:Clone()
				M4clone.Parent = player.Backpack
			    elseif timeplayed >= 50 then
				local G36C = game.ReplicatedStorage.G36C
				local G36Cclone = G36C:Clone()
				G36C.Parent = player.Backpack
				elseif timeplayed >= 100 then
				local HK416 = game.ReplicatedStorage.HK416
				local HK416Cclone = HK416:Clone()
				HK416.Parent = player.Backpack
			    elseif timeplayed >= 200 then
				local RPK = game.ReplicatedStorage.RPK
				local RPKCclone = RPK:Clone()
				RPK.Parent = player.Backpack
				elseif timeplayed >= 300 then
				local UMP45 = game.ReplicatedStorage.UMP45
				local UMP45Cclone = UMP45:Clone()
				UMP45.Parent = player.Backpack

end
end
end)
but theres one error:
ServerScriptService.Script:17: attempt to compare number and Instance
and i dont know how to fix it

Place a copy of the weapons in StarterGear.

I don’t want it like that sorry

Well, you could create a folder on the server that stores the weapons, something like this:
image