How can I make this jump script not rely on the spacebar?

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!

2 Likes

You can’t secure something that’s based completely on client input.

Didn’t mean “exploitable” as something that can be used by exploiters but I mean that someone can just put on a macro set at their spacebar to gain more jumps

What I said isn’t any less relevant, you can’t detect the player using automation to gain points or whatever, when it’s literally this easy.

1 Like

Understood. Is there any way to make the script support spacebar long presses?

There is a Humanoid.Jumping event. It fires with an argument of true when the player jumps and false shortly afterward. (It actually signals the humanoid state changing to and from Jumping, but it’s otherwise a reliable indicator of jumping)
It also works on “long presses” because it fires on each jump, not on spacebar, and also works on mobile because mobile has no spacebar.