Help With Leaderstats

Hey! I have this in a server script in a part, but I don’t know how to add points to something in the player’s leaderstats due to not referencing player.

script.Parent.Touched:Connect(function(hit)
	hit.Parent.HumanoidRootPart.Position = game.Workspace.Teleports.SpawnTP.Position
end)
1 Like

Change the code to this:

script.Parent.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
      local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	  hit.Parent.HumanoidRootPart.Position = game.Workspace.Teleports.SpawnTP.Position
    end
end)

This will make sure its a player’s character and then the plr variable references the player

Then you can do something like:

plr.leaderstats.Points.Value += 1

With points being the example stat

1 Like

To add something to the leader stats use this script

player.leaderstats.Gold.Value =  player.leaderstats.Souls.Value + 1 

Gold is an example

1 Like

I know how to do that, I was asking something else that I got the answer to.

1 Like

Oh. Sorry.


Well, here’s your options.
You can use:

local Plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) --Main Character

or

local Plr = game.Players[hit.Parent.Name]

Which is a bit less efficient, but still works.

And I recommend adding:

if hit.Parent:FindFirstChild("Humanoid") then
--do stuff
end

to make sure you know it’s a player/NPC
Then you can check if it’s a player, like @domboss37 stated, then go.
plr.Leaderstats.WhateverValueYouWant.Value = WhateverValueYouWantToSet