.Touched event only seems to run in Studio and not in a Game or Test Server?

I’m creating a weapon system for my game but I’m having a lot of trouble with it. There are no errors.

Currently the Touched event in the weapons doesn’t seem to run apart from in Studio ( when I click play ).

STUDIO
Studio

Game / Test Server
game

This is the code in my weapon:

local Swinging = false
local Weapon = game.ReplicatedStorage.Weapon
Weapon.OnServerEvent:Connect(function(player, combo)
    Swinging = true
end)

script.Parent.Touched:Connect(function(hit)
    if Swinging then
	    Swinging = false
	    if hit.Parent:FindFirstChild("Humanoid") then
		    if game.Players:GetPlayerFromCharacter(hit.Parent) then
		    else
			    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
		    end
	    end
    end	
end)

Am I doing something wrong? The Remote Event fires because I’ve tested it by printing the combo. I just don’t understand why it does’t work apart from in Studio.

The local script code that fires the Remote Event Weapon works fine so I don’t need to post that.

1 Like

Any errors?

Is it doing any Damage at all?

Try printing inside the TouchedEvent

Print everything

Example

if true then
print(1)
Debounce = false
if true then
print(2)
end
end

Sorry, I am on mobile so no indenting

No errors.

It does damage when I test in studio but not in an actual game or test server.

try Debugging in the TouchedEvent

Sorry, what do you mean?

try printing everything in the TouchedEvent

script.Parent.Touched:Connect(function(hit)
	print(1)
	print(hit)
    if Swinging then
		print(2)
	    Swinging = false
	    if hit.Parent:FindFirstChild("Humanoid") then
			print(3)
		    if game.Players:GetPlayerFromCharacter(hit.Parent) then
				print(4.1)
		    else
				print(4.2)
			    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
		    end
			print(5)
	    end
		print(6)
    end
	print(7)
end)

I’ve already tried that. It prints when I test in studio but not in an actual game or test server. Should of put that in the post, sorry.

that means that you either didn’t Connect the Event correctly or the part is not Touching anything at all

If I didn’t connect the event correctly or not actually touching a part, it wouldn’t be working when I test in Studio.

Yes but, Studio and an Actual Server is different, by not doing it correctly I mean something must have gone wrong so it broke on an Actual Server

From my code, does the event look connected properly?

Not really sure how to test if the part is actually connecting seeing as I have a line of humanoids and I’m running into them spamming left click so I’d presume my sword that’s in them is connected.

Does the server event to set swinging to true even get fired?

Also I’m noticing that swinging only gets set to false once someone is hit by the sword. It should also be set to false when the sword is done swinging.

I’ll test in a second; but just a quick question which could be the cause of my problem.

As I said, I have :OnServerEvent in the script inside the weapon which is inside my player.

Does :OnServerEvent only work in a script inside ServerScriptService or can it work in parts inside the workspace?

Just tested, Swinging is being changed.

It can be on server scripts (aka regular scripts) as long as they exist on the server.

Alright, that’s not my problem then. Swinging does get changed.

No, you can use it from any Script(not Local Script)

but your issue might be

Using only one RE Obj and Having OnServerEvent in multiple scripts

Not entirely sure tho, but that might be the case

(I don’t really know because I only have 1 RE and 1 RF for my Entire game)

also can you replace this with your script?

script.Parent.Touched:Connect(function(hit)
	print(Swinging , hit.Parent , hit.Parent:FindFirstChildOfClass("Humanoid") , game.Players:GetPlayerFromCharacter(hit.Parent))
    if Swinging and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
	end
end)

@RuizuKun_Dev is right that the event being in replicated storage will cause issues. Good catch, I hadn’t noticed.

However the issue here isn’t being caused by that. That will just make it so that when any client fires the event, all players’ swords will have swinging activated.

I’m lost on what would be causing this issue. Are you sure no errors are being thrown? It would be thrown on server.

No errors whatsoever which is why I’m so confused.

Would I be able to provide the file because I’ve been stumped on this since lunch ( and it’s now 7pm ;p )?

I wouldn’t post it here if you’re worried about code being stolen. You can message me it if you want and I’ll try to see what’s wrong, but i usually have trouble reading other people’s code no matter how well organized.