I need help with a script (If they player dies give them 10 coins)

So I am working on a game with a friend and we have run into a problem in our game we need it that when the player dies they are given +5 coins onto what they already got.
Our script has no errors and we can’t figure out why its not working.
We got a leader stat script witch is working all the problem is when the player dies they are not given +5 coins.

Here is our script.
local players = game:GetService(“Players”)

players.PlayerAdded:Connect(function(plr)

task.spawn(function()

	local player = plr

	repeat task.wait() until player.Character

	local hummanoid = player.Character:FindFirstChild("Humanoid")

	hummanoid.Died:Connect(function()
		local humanoid = game.Players:GetPlayerFromCharacter()
		if humanoid then
			if not humanoid then
				humanoid.leaderstats.Coins.Value = humanoid.leaderstats.Coins.Value + 50
			end
		end
	end)
end)

end)

We have put 50 for testing.

???

This should’ve been PAINFULLY obvious

Also save yourself some slack and use compound operators:

humanoid.leaderstats.Coins.Value = humanoid.leaderstats.Coins.Value + 50
--is the same as
humanoid.leaderstats.Coins.Value += 50
2 Likes

I feel like a dummy for not seeing that.
Do you know how I would fix this.

task.spawn(function()

	local player = plr -- THIS SCRIPT WILL WORK IF "plr" IS PLAYER INSTANCE

	repeat task.wait() until player.Character

	local hummanoid = player.Character:FindFirstChild("Humanoid")

	hummanoid.Died:Connect(function()
		if player then
				player.leaderstats.Coins.Value += 50
		end
	end)
end)

this entire script is honestly painful to read lol

Firstly, you already have the player in the players added parameters.

Secondly, what i would do is something like this

players.PlayersAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
   local humanoid = character.Humanoid
   repeat task.wait() until humanoid

      if humanoid then
        humanoid.Died:Connect(Function(humanoidParameter)
        points.Value += 5
      end
   end
end)

the problem im seeing with your code is this part here

  1. Why are you running that part of the code? if humanoid then will run if the humanoid exists, but the if not humanoid then will run if the humanoid doesnt exist. Adding the points if the humanoid doesnt exist wont work.

(I JUST REALIZED THAT THE HUMANOID VARIABLE IS SUPPOSE TO BE A PLAYER)

You need to put a players character model into the parameter, you cant just run players:GetPlayerFromCharacter() without a character paremeter

the code i showed above is pseudo code, i just woke up so it probably wont work or make any sense but its a basic idea

I just tried your way and it still doesn’t work.
I do feal dumm for not knowing why my script wasn’t working.

game.Players.PlayerAdded:Connect(function(p)
    print(p.Name.." Has Joined the Game. Finding Humanoid...")
    local Humanoid = p.Character:WaitForChild("Humanoid")
    if Humanoid then
        print("Found Humanoid. Now waiting for Humanoid.Died")
        Humanoid.Died:Connect(function()
            p.leaderstats.Coins.Value += 50
            print("Gave "..p.Name.." 50 Coins as He/She Died")
        end)
    else
        print("Could'nt Find the Humanoid for "..p.Name)
  end
end)

Also just incase this doesn’t work (Debug Instructions)

Check your output. And Try Debugging using the prints provided.

Check the script for making the player die or just jump off the baseplate or whatever ur map is. Just trigger the Death of Humanoid.

Check the Leaderstats script.
Make sure the Name of Folder you make on player joining is “leaderstats” or the Coins won’t appear in the Leaderstats.

Observe the Value and check the properties panel to view it’s value.

I Hope this works out for you Happy Coding :slight_smile:

local function CharacterAdded(plr, char)
    local hum = char:WaitForChild("Humanoid")
    local leaderstats = plr:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")

    if not leaderstats or not coins then return end

    hum.Died:Connect(function()
        coins.Value += 5
    end)
end

game.Players.PlayerAdded:Connect(function(plr)
    local char = plr.Character

    if char then
        CharacterAdded(plr, char) -- character may load before the event
    end

    plr.CharacterAdded:Connect(function(char)
        CharacterAdded(plr, char)
    end)
end)
1 Like

What do you mean by check the value?
I just got a script that adds a part to the leader board that’s named EndPoints.

bruh.

I Dont think u understand how leaderstats work.i am sorry if this doesnt solve the problem though lets just hope for best.

If you make a folder under a “Player” Instance, and you name it exactly “leaderstats” that folder will start to refer as the ingame stats which show players. Now adding an IntValue or a StringValue will create a new field based on its name. Here you are adding a part and it wont work as it isnt a value.

I think you should rework ur Script.