Hello, I have this script that currently works like this:
if a player presses the spacebar and jumps the leaderstats value goes up by +1 but the problem is that it’s easily exploitable and doesn’t count the jumps when the spacebar is long pressed.
How can I fix those problems?
Script:
local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end)
return data
end
local function saveData(datastore, key, value)
local success, err = pcall(function()
datastore:SetAsync(key, value)
end)
if err then print(err) end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local debounce = true
humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
if debounce == true then
debounce = false
if humanoid.Jump == true then
jumpCount.Value = jumpCount.Value + 1
end
wait(0.2)
debounce = true
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end)
Thanks in advance!