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)
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
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)