Problem with sword damage...I think

hey guys,

**First of all I want to say that I have absolutely no idea what is going so I might have some troubles explaining what my problem is.
A script seems to run x amount of times, depending on the amount of players online.
**

So I made a tool and put in ServerStorage, to use later.
serverstorage

When a player joins I make a folder and put that model in there, with an unique id.
I cloned it and renamed it.
serverstorage folderµ

I forgot to say, I use a GUI to clone the tool and put in my backpack…

Normally, when I activate my tool, 1 hitbox should appear, like shown below

But for example when I test using 2 players…

Looking it up online is hard because I have no idea what to search for.

this server script gets run twice!

wait(1)
local damage = 15
local swordHandler =  game:GetService("ReplicatedStorage"):WaitForChild("swordHandler")

swordHandler.OnServerEvent:Connect(function(player, extraDmg)
	print("event fired")
	local character = player.Character
	local id = player.UserId
	
	--hit part
	local hitbox = Instance.new("Part")
	hitbox.Name = "Hitbox" 
	hitbox.Size = Vector3.new(5,5,5)
	hitbox.CanCollide = false
	hitbox.CFrame = character.PrimaryPart.CFrame
	hitbox.Parent = workspace
	hitbox.Massless = true
	hitbox.Transparency = 0
	hitbox.Anchored = false
	print("test")

	
	
	local weld = Instance.new("Weld")
	weld.Part0 = character.PrimaryPart
	weld.Part1 = hitbox
	weld.C0 = CFrame.new(0,0,-5)
	weld.Parent = weld.Part0
	
	
	
	local damageDealt = false
	hitbox.Touched:Connect(function(hit)
		if not damageDealt then
			if hit.Parent == character then return end
			if not hit and not hit.Parent then return end
			
			local human = hit.Parent:FindFirstChild("Humanoid")
			if human and human.Health > 0 then
				if extraDmg then
					human:TakeDamage(damage + damage/2)
				else
					human:TakeDamage(damage)
				end
				
				damageDealt = true
			end
		end
		print(damage)
	end)
	
	
	--slash effect
	local slash = game.Lighting.Slash:Clone()
	slash.Parent = hitbox
	slash.CFrame = hitbox.CFrame
	slash.Orientation = character.PrimaryPart.Orientation
	slash.BodyVelocity.Velocity = character.PrimaryPart.CFrame.LookVector * 50

	local x = 0
	while x < 5 do
		slash.Transparency += .5
		x+=1
		--wait(0.05)
		wait(545454545)
	end
	hitbox:Destroy()
end)

this is the client script

local players = game:GetService("Players")

--References
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

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


--Animations
local atk1 = humanoid:LoadAnimation(script.Parent.Animation:WaitForChild("atk1"))
local atk2 = humanoid:LoadAnimation(script.Parent.Animation:WaitForChild("atk1"))
local atk3 = humanoid:LoadAnimation(script.Parent.Animation:WaitForChild("atk1"))

--Variables
local click = 0
local lastClick = 0
local timeBetween = 0.5
local debounce = false

tool.Activated:Connect(function(player,isTyping)
	if isTyping then return end
	if humanoid.Health == 0 then return end
	if not debounce then
		debounce = true
		
		click += 1
		if click >= 3 then
			click = 0
			swordHandler:FireServer(true)
			atk3:Play()
			wait(1.5)
			click = 0
			
		elseif click == 1 then
			swordHandler:FireServer(false)
			atk1:Play()
		elseif click == 2 then 
			swordHandler:FireServer(false)
			atk2:Play()	
		end
		
		wait(timeBetween)
		debounce = false
		
		wait(2)
		lastClick = click
	end
end)

while true do
	wait(5)
	if lastClick == click then
		click, lastClick = 0,0
	end
end

I think it must be a problem with one of those.
I’m sorry for bombarding you with those scripts and pics etc.
But I have no idea what to do.

If someone can push me in the right direction on how to fix it, that would be highly appreciated. Even if it’s just a thought.
If you need more information , please ask me

1 Like

If you are giving the sword through a GUI, are you using a local script? If so it will only appear on the players screen who ran the local script.

This is happening because you are using 2 server scripts when you clone 2 tools. This means that every time you clone the tool another connection is made to receive the remote. To fix this, take the part where you listen to the client(server script) and put it in server script service.

that seems to fix it, I never would have thought putting a damage script in server script service. But it works, Thank you

1 Like