Help with only damaging once

So I have a tool that shoots a projectile which creates a projectile on every client and would damage the player but it would cause the projectile to damage multiple times depending on the amount of players in the game.

Client:

local function DamagePlayer(hit)
	local character = hit:FindFirstAncestorWhichIsA("Model")

	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if humanoid and player and OwnerValue.Value and OwnerValue.Value.Team ~= player.Team then
			Remotes.DamageHumanoid:FireServer(humanoid, 10)
		end
	end
end

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

Remotes.DamageHumanoid.OnServerEvent:Connect(function(player, humanoid, damage)
	if humanoid then
		humanoid:TakeDamage(damage)
	end
end)

How would I solve this?

1 Like

maybe adding a debounce?

I haven’t tested this code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local debounceTbl = {}

Remotes.DamageHumanoid.OnServerEvent:Connect(function(player, humanoid, damage)
	local lastHitTime = 0
	if debounceTbl[player.UserId] then
		lastHitTime = debounceTbl[player.UserId]
	end
	if lastHitTime >= (os.time() - 1) then return end --adding a debounce of 1 second
	if humanoid then
		lastHitTime[player.UserId] = os.time()
		humanoid:TakeDamage(damage)
	end
end)
1 Like

It doesn’t seem like an efficient way.

1 Like

I agree, but I didn’t understand the context of your problem “projectile on every client”

1 Like

I’ve actually just figured that I could do this:

local function DamagePlayer(hit)
	local character = hit:FindFirstAncestorWhichIsA("Model")

	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		local plr = game.Players:GetPlayerFromCharacter(character)

		if humanoid and plr and OwnerValue.Value and OwnerValue.Value.Team ~= plr.Team and OwnerValue.Value == player then
			local damage = humanoid.Health
			Remotes.DamageHumanoid:FireServer(humanoid, damage)
		end
	end
end

OwnerValue.Value == player

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.