sorry if this is unclear or isnt written correctly, as this is my first post.
ive been trying to make a system to where if you click, the client detects it, and sends it over to the server using a remoteevent, to update a “punches” variable
the issue is, when i do this, the removeevent gets fired more and more times every time i click
it also starts out firing like 80 times
1 click - 80 fires
2 clicks - 160 fires
you get the idea
i need it to be for every click, a remoteevent gets fired once.
ive tried everything that comes to my mind as a new roblox dev, like debounces, but nothing seems to work
clientside:
local uis = game:GetService("UserInputService")
local debounce = true
local run = Instance.new("IntValue")
run.Value = 0
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("mouse down")
wait()
if debounce == true then
wait()
game.ReplicatedStorage.Punch:FireServer()
run.Value += 1
print(run.Value)
debounce = false
print(debounce)
uis.InputEnded:Connect(function(input)
print(input.UserInputType)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("mouse up")
wait(0.2)
debounce = true
end
end)
end
end
end)
server script:
local enterEvent = game.ReplicatedStorage.Going_In
local punchEvent = game.ReplicatedStorage.Punch
enterEvent.OnServerEvent:Connect(function(player)
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local activated = Instance.new("IntValue")
activated.Value = 0
while humanoid.Health > 0 do
wait()
punchEvent.OnServerEvent:Connect(function(player,debounce)
print("recieved")
activated.Value += 1
print(activated.Value)
print(debounce)
local leaderstats = player:WaitForChild("leaderstats")
local punches = leaderstats:WaitForChild("Punches")
punches.Value += 1
end)
end
end)
Nevermind, this part of the code is the issue. You are connecting the punchEvent every wait(). Remove the loop and try not using two remote events when you can use one.
punchEvent.OnServerEvent:Connect(function(player,debounce)
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local leaderstats = player:WaitForChild("leaderstats")
local punches = leaderstats:WaitForChild("Punches")
punches.Value += 1
end)
i created enterEvent to send a signal every time your character teleports into the map through a portal, and i detect every time the player punches with punchEvent.
is there another way to know when the player entered the map through the portal, and only fire punch once you used the portal?
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local enterEvent = ReplicatedStorage.Going_In
local punchEvent = ReplicatedStorage.Punch
local playersEntered = {}
-- Handling portal
enterEvent.OnServerEvent:Connect(function(player)
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
table.insert(playersEntered, player)
end)
-- Handling punching
punchEvent.OnServerEvent:Connect(function(player,debounce)
if table.find(playersEntered, player) then
local leaderstats = player:WaitForChild("leaderstats")
local punches = leaderstats:WaitForChild("Punches")
punches.Value += 1
end
end)
-- If a player leaves then remove them from the table
Players.PlayerRemoving:Connect(function()
if table.find(playersEntered, player) then
table.remove(playersEntered, table.find(playersEntered, player))
end
end)
You can connect them separetely and use a table to know who is in and who isnt. Use table.clear(playersEntered) if you bring all players back or something