Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make an accurate hit?

I'm making a game where you can slap people, and when you do, you get 1 point from it. There is a problem though. It is detecting a slap multiple times giving a lot more points than it should be.

Any help would be nice.

Here is my script. It's a local script btw.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://4617501015"
	local play = player.Character.Humanoid:LoadAnimation(animation)
	play:Play()
	script.Parent.Parent.Character["Right Arm"].Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild('Humanoid') then
			wait()
			hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
			script.Parent.Parent.leaderstats.Slaps.Value = script.Parent.Parent.leaderstats.Slaps.Value + 1
			script.Parent.Parent.leaderstats.Coins.Value = script.Parent.Parent.leaderstats.Coins.Value + math.random(2,7)
		end
	end)
end)

1 answer

✓ Accepted answer
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local debounce = true -- our debounce
local cooldown = 1 -- amount of time till you can hit again

mouse.Button1Down:Connect(function()
	if debounce then
    local animation = Instance.new("Animation")
    animation.AnimationId = "rbxassetid://4617501015"
    local play = player.Character.Humanoid:LoadAnimation(animation)
    play:Play()
        script.Parent.Parent.Character["Right Arm"].Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild('Humanoid') and debounce then
			debounce = false
            wait()
            hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
            script.Parent.Parent.leaderstats.Slaps.Value = script.Parent.Parent.leaderstats.Slaps.Value + 1
            script.Parent.Parent.leaderstats.Coins.Value = script.Parent.Parent.leaderstats.Coins.Value + math.random(2,7)
        end
     end)
	wait(cooldown) -- waits the amount of time for cooldown
	debounce = true -- enables it back so you can hit again
	end
end)

we can utilize debounces to make this fix, if this helped make sure to respond.

https://developer.roblox.com/en-us/articles/Debounce

1 comment

0

Thanks! I don't know how I didn't think of this.

This discussion is preserved as it was. New answers, comments, and votes are closed.