Help with leaderstat Script

I’m trying to write a script where if you click a key you get 15 points/xp. Yet it does not work. Could I have some help?


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)

function onKeyPress(actionName, userInputState, inputObject)
		    if userInputState == Enum.UserInputState.Begin then
			        game.leaderstats.XP.Value = 15
			    end
		end
	
	game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.Q)
game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local XP = Instance.new("IntValue", leaderstats)
	XP.Name = "xp"
end)

Thank you

So too add the Value you have too do this

game.leaderstats.XP.Value = game.leaderstats.XP.Value +15

1 Like

quick question would i put his script in starter gui as a localscript or a script in serverscritptserves

With this, don’t specify the parent yet.
Specify the name, make sure it’s “leaderstats”
Then you specify the parent.

1 Like

Server script. This is done in server script.

All it means is that it can’t go through the boundary’s of client/server

You can’t use UserInputService in this case, use a click detector.

1 Like

i might be wrong but if you use pc you can just use this but make sure u have the service like
@ItzMeZeus_IGotHacked said you need UserInputService or it woun’t work and if ur doing both mobile and pc just use BindToAction(my way of doing it()

function onKeyPress(actionName, userInputState, inputObject)
		    if userInputState == Enum.UserInputState. MouseButton1 then
			        game.leaderstats.XP.Value = 15
			    end
		end

This whole script is corrupted. Here, tru use this.

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new(“Folder”)
    leaderstats.Name = “leaderstats”
    leaderstats.Parent = player

    local points = Instance.new(“IntValue”)
    points.Name = “Points”
    points.Value = 0
    points.Parent = leaderstats
end

For the click detector script, I can’t help you.

For this you need a RemoteEvent.

Create a RemoteEvent in ReplicatedStorage
Create a LocalScript in StarterGui
Create a Script in ServerScriptService
Create a Script in Workspace

Create the leaderboard (Script in Workspace)

function CreateStats(NewPlayer)

local MainStats = Instance.new("IntValue")

MainStats.Name = "leaderstats"

MainStats.Value = 0

local PointsValue = Instance.new("IntValue")

PointsValue.Name = "Points"

PointsValue.Value = 0

PointsValue.Parent = MainStats

MainStats.Parent = NewPlayer

return MainStats

end

game.Players.ChildAdded:connect(CreateStats)

LocalScript in StarterGui

local UserInputService = game:GetService("UserInputService") 

UserInputService.InputBegan:Connect(function(input) 
	if input.KeyCode == Enum.KeyCode.W then
		local rs = game:GetService("ReplicatedStorage")
		
		local givepoint = rs:FindFirstChild("GivePoint")
		
		givepoint:FireServer()
	end
end)

Regular script in ServerScriptService

local rs = game:GetService("ReplicatedStorage")

local givepoint = rs:FindFirstChild("GivePoint")

givepoint.OnServerEvent:Connect(function(player)
 player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1	
end)

Hope this works!

You can change W and “Points” to anything, just make sure to change “Points” in all the scripts that have it. (except the RemoteEvent)

Edit: fixed error

2 Likes