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)
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.
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
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)
First, make a RemoteEvent in ReplicatedStorage. Let’s call it “ToolEvent”.
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)
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.