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
Game / Test Server
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.
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)
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.
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.
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.