Sword won't work in game but work in studio

  1. What do you want to achieve? Keep it simple and clear!
    My sword won’t work in game but it work in studio and I want to fix that

  2. What is the issue? Include screenshots / videos if possible!
    Sword won’t damage in game but damage in studio

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried searching for solution but still can’t figure out why it doesn’t work

Sword in game :

Sword in studio :

local deb = false
local combo = 1
local sound = script.M1
local soundhit = script.M1hit
local trail = script.Parent.Parent.Parent.Handle.Trail
local Particle = script.Parent.Parent.Parent.Handle.Attachment1.ParticleEmitter
local HiEffect = require(game.ServerStorage:WaitForChild("HitEffectModule"))

script.Parent.OnServerEvent:Connect(function(ply)
	if deb == false then
		deb = true
		
		local char = ply.Character
		local humrp = char:WaitForChild("HumanoidRootPart")
		local hum = char:WaitForChild("Humanoid")
		trail.Enabled = true
		Particle.Enabled = true
		
		if combo == 1 then
			local Anim = script.Swing1
			local Track = hum:LoadAnimation(Anim)
			Track:Play()
			combo = 2
		else
			local Anim = script.Swing2
			local Track = hum:LoadAnimation(Anim)
			Track:Play()
			combo = 1
		end
		
		sound:Play()
		
		local bv = Instance.new("BodyVelocity")
		bv.MaxForce = Vector3.new(10000,10000,10000)
		bv.Velocity = (humrp.CFrame.lookVector * 15)
		bv.Parent = humrp
		
		hum.WalkSpeed = 0

		local Hitbox = Instance.new("Part")
		Hitbox.Size = Vector3.new(4,5,5)
		Hitbox.CFrame = humrp.CFrame * CFrame.new(0,0,-3)
		Hitbox.CanCollide = false
		Hitbox.Anchored = false
		Hitbox.Transparency = 1
		Hitbox.Massless = true

		local weld = Instance.new("WeldConstraint")
		weld.Part0 = humrp
		weld.Part1 = Hitbox
		weld.Parent = humrp

		Hitbox.Parent = char
		
		local canHit = true
		
		Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent ~= char then
					if canHit == true then
						canHit = false
						local thumrp = hit.Parent.HumanoidRootPart
						
						HiEffect.Gore2(thumrp)
						
						soundhit:Play()
						
						local bv = Instance.new("BodyVelocity")
						bv.MaxForce = Vector3.new(11000,11000,11000)
						bv.Velocity = (humrp.CFrame.lookVector * 50)
						
						bv.Parent = thumrp
						hit.Parent.Humanoid:TakeDamage(22)
						wait(0.1)
						bv:Destroy()
					end
				end
			end
		end)
		
		wait(0.2)
		
		bv:Destroy()
		Hitbox:Destroy()

		deb = false
		
		wait(0.1)
		
		hum.WalkSpeed = 16
		trail.Enabled = false
		Particle.Enabled = false
	end
end)

The code is in a script in the tool

1 Like
local trail = script.Parent.Parent.Parent.Handle.Trail
local Particle = script.Parent.Parent.Parent.Handle.Attachment1.ParticleEmitter
local HiEffect = require(game.ServerStorage:WaitForChild("HitEffectModule"))
local deb = false
local combo = 0

script.Parent.OnServerEvent:Connect(function(ply)
	if deb == false then
		deb = true
		local char = ply.Character
		local humrp = char:WaitForChild("HumanoidRootPart")
		local hum = char:WaitForChild("Humanoid")
		trail.Enabled = true
		Particle.Enabled = true
		if combo == 1 then
			local Anim = script.Swing1
			local Track = hum:LoadAnimation(Anim)
			Track:Play()
			combo = 2
		else
			local Anim = script.Swing2
			local Track = hum:LoadAnimation(Anim)
			Track:Play()
			combo = 1
		end
		--sound:Play()
		local bv = Instance.new("BodyVelocity")
		bv.MaxForce = Vector3.new(10000,10000,10000)
		bv.Velocity = (humrp.CFrame.lookVector * 15)
		bv.Parent = humrp
		hum.WalkSpeed = 0
		local Hitbox = Instance.new("Part")
		Hitbox.Size = Vector3.new(4,5,5)
		Hitbox.CFrame = humrp.CFrame * CFrame.new(0,0,-3)
		Hitbox.CanCollide = false
		Hitbox.Anchored = false
		Hitbox.Transparency = 1
		Hitbox.Massless = true
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = humrp
		weld.Part1 = Hitbox
		weld.Parent = humrp
		Hitbox.Parent = char
		local canHit = true
		Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent ~= char then
					if canHit == true then
						canHit = false
						local thumrp = hit.Parent.HumanoidRootPart
						HiEffect.Gore2(thumrp)
						--soundhit:Play()
						local bv = Instance.new("BodyVelocity")
						bv.MaxForce = Vector3.new(11000,11000,11000)
						bv.Velocity = (humrp.CFrame.lookVector * 50)
						bv.Parent = thumrp
						hit.Parent.Humanoid:TakeDamage(22)
						wait(0.1)
						bv:Destroy()
					end
				end
			end
		end)
		wait(0.2)
		bv:Destroy()
		Hitbox:Destroy()
		deb = false
		wait(0.1)
		hum.WalkSpeed = 16
		trail.Enabled = false
		Particle.Enabled = false
	end
end)

The script references both “deb” and “combo” but neither are declared & initialised, similarly an attempt is made to play 2 “Sound” instances which are also not assigned to declared variables. I’ve added “deb” and “combo” and commented out the attempt to play both “Sound” instances, try this.

1 Like

So,script.Parent is a Remote Event, is the script itself a local or server script?

Add a print line at the start of your OnServerEvent and the caller in your local script to find which is not working.

Then keep moving the print forward through the script til it doesn’t output.
Even read your code out loud so you truly read through it.

So the script is a child of the remote event that is a child of a local script but the thing is that everything work fine on studio but it won’t damage in the real game for some reason

Tried the script, it works fine in studio but still won’t work in game and also when I tested it in the game the hitbox is working fine but it doesn’t damage the dummy like in studio for some reason
It’s like the script is having some issue with the touch event but I still don’t know how to fix it

the sword effect look cool. I might try make one :star_struck:
maybe try not to parent the server script to localscript

Try moving the Server script outside of the Remote and Local script.

I don’t believe Local scripts replicate to the server.

-Edit- also move the Remote outside of the Local script aswell

I would recommend doing hitreg on the client, passing the information on to the server and performing server sided sanity checks to prevent exploiters. If you or the player you are attacking have high ping, the chances of hits registering are very low.

I might try that soon because my game will be based on PVE more than PVP. I haven’t tried any of the solutions yet but I would really want to know what cause it so I won’t make the same mistake in the future

I think it is caused due to the server sided hit detection. This problem happened to me while I was working on a project. I fixed it by doing the hit detection on the client and doing checks + damage on the server.

1 Like

If team create is enabled in ur place, then commit the script draft to save it

Store debounce for each player inside a table

local debounces = {}
script.Parent.OnServerEvent:Connect(function(ply)
	if debounces[ply] then return end
		debounces[ply] = true
		--code--
		wait(n)
		debounces[ply] = nil or --debounces[ply] = false
	end
end

I Fixed it some how, I just deleted all the hitbox lines, the local script, make the sword run only using 1 server script and use the sword handle for the touch event instead and that worked. Thanks for all the suggestion.