Get Player Leaderstats Value With Script

  1. **What do you want to achieve?

so if you play arsenal , you will know they need 31 kills to win . what i want is to get the player kills using server scripts .

  1. **What is the issue?

u can’t use player.localplayer in a server script so i have no idea how i can get player kill value

  1. **What solutions have you tried so far?

the script below this is what i can think of

local leaderstats = game.players.localplayer.leaderstats.kills.value
local remote = game.replicatedstorage:findfirstchild(“remoteEvent”)

repeat wait() until leaderstats.Value == 31

remote:FireAllClients(true)
wait(5)
remote:FireAllClients(false)

5 Likes

When the player makes a kill send a remote event to the server, from their the first parameter in the remote event is the player that sent it.

2 Likes

Code:

game.Players.PlayerAdded:Connect(function(player)
    kills = player:WaitForChild("leaderstats").kills
    kills.Changed:Connect(function(newValue)
        if newValue >= 31 then
              print("Player won the round")
       end
    end)     
end)

I think this might work.

9 Likes

is there a more easy way to do this??

1 Like

ok i will try it now . thanks for the script

3 Likes

Yes, you can use a Value.Changed event instead of doing a loop to check for when the value hits your determined amount and then send your events/end the round.

Edit: personally, I’d use a for loop to check all the players kill values on the server and only fire a remote event if I was updating a GUI. This keeps everything more secure as the count stays on the server at all times.

3 Likes

Remote events, are pretty easy to use. But their is another way using the change event.

Kills.Changed:Connect(function()
If Kills.Value == 31 then
—What ever you want to do if player reaches 31 kills
end
end

Handle this server-side.

1 Like
local Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("kills") -- Kills is Leaderstats Name
Leaderstats:GetPropertyChangedSignal("Value"):Connect(function()
    if Leaderstats.Value == 31 then
        print("Value is 31 - Game End")
   end
end)

This should be a LocalScript.
Instead of a Loop, Use GetPropertyChangedSignal.

Also, Make Sure That Your Stats Name is kills with a Small k.
If Not, Change the

local Leaderstats = game.Players.LocalPlayer:WaitForChild(“leaderstats”):WaitForChild(“kills”)

To

local Leaderstats = game.Players.LocalPlayer:WaitForChild(“leaderstats”):WaitForChild(“Kills”)

3 Likes

This shouldn’t be handled on the client. It would make the game extremely vulnerable to exploiters who want to win the game for themselves. Using a loop to check the players leader stat values on the server and finding the winner that way is a lot more reliable @HHeartlessHunteR

3 Likes

This issue is different than enough from that topic that it shouldn’t be included, please don’t bring any unrelated topics into this one. Just a fourm tip.

2 Likes

Well If That’s the Case, He Can Use A Couple of Ways Too.

local PlayerWhoWon -- Blank Variable
while wait() do
    if PlayerWhoWon == nil then -- No Winner Yet
       for _, LocalPlayer in pairs(game.Players:GetChildren()) do
        if LocalPlayer:FindFirstChild("leaderstats") then
         if LocalPlayer.leaderstats:FindFirstChild("Kills") then -- Capital K 
             if LocalPlayer.leaderstats.Kills.Value == 31 then -- Capital K | 31 Kills 
               PlayerWhoWon = LocalPlayer.Name -- Set The Variable to The Winners Name
               break -- Break Loop
             end
           end
         end
       end
     else
     break -- Break the Loop if Found a winner
   end
end
4 Likes

Just use module scripts. So much more helpful and secure.

2 Likes

its not working … there is no error as well

1 Like

where do you put this scripts ?

1 Like

I would use this. Server script in SeverScriptService.

for i,v in pairs(game.Players:GetChildren()) do
	local leaderstats = v:WaitForChild("leaderstats")
      local kills = leaderstats:WaitForChild("kills")
	kills.Changed:connect(function()
      if kills == 31 then 
            --do whatever you wanna do
            end
	end
end

Might be some minor errors since I typed this out from memory.

2 Likes

O just edited the script, try it now.

1 Like

still not working , is it cuz i directly change the value from explorer tab?

1 Like

Are you changing the value from the client or server side? It has to be the server to work. Also, what are the errors in the output if you have any from the script I gave you?

1 Like

umm idk . what i know is i change the value from explorer tab . there is no error as well .

1 Like

You need to use another script to change the value within your game, editing it while not in a play test of studio isn’t ever going to fire the event.

1 Like