How to check for a player and access their leaderstats

Hello, i have a problem where my code doesnt add strength to my players leaderstats value called “Strength”, this is my code.

local dumbell = script.parent

dumbell.Activated:Connect(function(player) -- i think this is the problem because i never specified what 'player' is, but i dont know how to specify if its in the player service.

player.leaderstats.strength.Value = playr.leaderstats.strength.Value + 1

end)

im pretty stupid and tried this aswell:

local dumbell = script.parent
local playerservice = game:GetService("Players)"

dumbell.Activated:Connect(function(player) -- i think this is the problem because i never specified what 'player' is, but i dont know how to specify if its in the player service.

local player = script.Parent.Parent.Name

local RealPlayer = playerservice:FindFirstChild(player)

if RealPlayer then
     RealPlayer:FindFirstChild("leaderstats").strength.Value = RealPlayer:FindFirstChild("leaderstats").strength.Value + 1


end)
1 Like

Hello! It seems like you’re trying to increase the “Strength” value of a player’s leaderstats when an object (dumbell) is activated. The issue you’re facing is likely due to the player parameter in the Activated event not being the actual player instance you’re expecting.

In Roblox, the Activated event for a ClickDetector passes the player who activated it as the first argument. So, you can use this directly to reference the player.

So i use a clickdetector instead of the activated event?

Yes, the Activated event is associated with a ClickDetector object. This object is typically parented to the part you want to be clickable (in your case, the “dumbell”). When a player clicks this part, the Activated event fires and passes the player who clicked as an argument.

So, you would use a ClickDetector and its Activated event together

1 Like

so basically dumbell.clickdetector.activated or dumbell.clickdetector.mouseclick1

Hi!
Is the “dumbell” reffering to the Tool? (it has the same event)

1 Like

The .Activated event is not actually associated with ClickDetector.
All of the events of this instance can be found here

Oh really? My apologies. I thought it was like that because i did something similiar like that before and it worked.

1 Like
local dumbbell = script.parent
local playerservice = game:GetService("Players")

dumbbell.Activated:Connect(function()
    local player = dumbbell.Parent
    local realPlayer = playerservice:WaitForChild(player.Name)
    local leaderstats = realPlayer:WaitForChild("leaderstats")
    local strength = leaderstats:WaitForChild("strength")
    strength.Value = strength.Value + 1
end)

very raw

If this is a server-side Script, that definiteky won’t work. You are checking for one singular tool which is probably in StarterPack.

I’d recommend managing activation on the client, and using a RemoteEvent to add the strength server-side.

how do i do that with a remote event i never use them lol

First, make a RemoteEvent in ReplicatedStorage. Let’s call it “ToolEvent”.

  1. LocalScript in your tool, which I presume is under StarterPack:
local players = game:GetService("Players")
local rStorage = game:GetService("ReplicatedStorage")

local event = rStorage:WaitForChild("ToolEvent")
local player = players.LocalPlayer

local tool = script.Parent

local function send()
    event:FireServer()
end

tool.Activated:Connect(send)
  1. Script in ServerScriptService:
local rStorage  = game:GetService("ReplicatedStorage")
local event = rStorage.ToolEvent

local function addStrength(player:Player)
    local var = player.leaderstats.strength
    var.Value += 1
end

event.OnServerEvent:Connect(addStrength)

quick note: the first parameter for OnServerEvent will always be the player that fired the event, regardless of whether you passed that parameter or not.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.