How To Increase Player Stats

Hello again, I am trying to increase a player stat by 1 and kill the player when he/she touches a part. this script doesn’t seem to work. This script currently kills the player but doesn’t increase his value. This script is located in a regular Script. I tried putting it in a LocalScript but that made it worse, the player neither dying nor increasing in stat.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

function player(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid ~= nil then
		Humanoid.Health = 0
		player.leaderstats.Deaths = player.leaderstats.Deaths + 1
	end
end

script.Parent.Touched:Connect(player)
1 Like

Do you have any security holes there, by the way, is it a LocalScript or a Script?

game.Players.LocalPlayer Does not work on Scripts, only LocalScripts.

You must also change the name of your function, because it and the variable “player” are covering up, so when you connect the function, nothing happens.

game.Players.LocalPlayer is not available in server Scripts. Put local player = Players:GetPlayerFromCharacter(hit.Parent) inside of the function instead and remove the player variable from the top of the script.

like that:
I dont quite understand how i would go about doing that

local player = Players:GetPlayerFromCharacter(hit.Parent)

function player(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid ~= nil then
		Humanoid.Health = 0
		player.leaderstats.Deaths = player.leaderstats.Deaths + 1
	end
end

script.Parent.Touched:Connect(player)

Try defining the player within the function

You need to do Deaths.Value not just referencing the object. I’d also recommend not increasing it from the kill script and instead hooking a .Died event onto the Humanoid. This is best so there aren’t any accidental reruns of the touched function.

local Humanoid
Humanoid.Died:Connect(function()
– stat change
end)

2 Likes

Your new script would look like:

local Players = game:GetService("Players")

function player(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if Humanoid ~= nil and player then
		Humanoid.Health = 0
		player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
	end
end

script.Parent.Touched:Connect(player)

Note: This does not reflect Sovereign’s recommendation.

Alright ! It now works, although it increases the stats non stop when its touching the part (it went to 17 in a second) I’m think a wait(3) might do it?

edit: I added a wait and it didnt work.

No, please use Sovereign’s recommendation. It would look somewhat like:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
		end)
	end)
end)

I would recommend adding this to the script that creates the leaderstats.

Edit: Events will fire even if there is a wait. I would recommend putting a denounce in the script by the way.

Here is the final script: it still gives me more than 1 deaths when i touch the part

local Players = game:GetService("Players")

function player(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if Humanoid ~= nil and player then
		Humanoid.Health = 0
		player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
		
	end
end

script.Parent.Touched:Connect(player)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
		end)
	end)
end)

Take the first appearance of player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1 out. As I said in my last post, I would recommend adding the last part to the script where you made the leaderstats.

Thank you so much! It works now!