local RS = game:GetService("ReplicatedStorage") local SwordDamage = RS:WaitForChild("SwordDamage") local Sword = game.StarterPack.Sword.Handle local debounce = false SwordDamage.OnServerEvent:Connect(function() print(1) Sword.Touched:Connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") and not debounce then debounce = true print(2) Hit.Parent.Humanoid:TakeDamage(10) wait(0.5) debounce = false end print(3) end) print(4) end)
The problem here is that whenever I call the remote event, the damage function won't work ( the 2nd function the middle). When I would run it there would be no errors in the output and would print only "1" and "4". How do I get this to work?
Hey there, unidentifiedw
Are you using any custom characters that don't have humanoids? Is the sword hitbox maybe not big enough? Are you getting an infinite yield from the variables in the top of the script? I went ahead and tested this in my studio with a part and it worked just fine.
Here is the code that I used:
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local debounce = false ReplicatedStorage.test.OnServerEvent:Connect(function() print(1) script.Parent.Touched:Connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") and not debounce then debounce = true print(2) Hit.Parent.Humanoid:TakeDamage(10) wait(0.5) debounce = false end print(3) end) print(4) end)
Client:
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input, gpe) if gpe then return end if input.KeyCode == Enum.KeyCode.Q then game:GetService("ReplicatedStorage").test:FireServer() end end)
I just used UserInputService
for testing purposes so I could fire the remote event when I press a key.
As always, if this helped you out make sure to upvote it, if this solves your issue than make sure to mark this as the answer so I can earn that sweet sweet reputation and other people on the forums don't mistake this thread for an active discussion.
You put the handle as the starter gear handle not the player's actual handle try this
local RS = game:GetService("ReplicatedStorage") local SwordDamage = RS:WaitForChild("SwordDamage") local Sword local debounce = false SwordDamage.OnServerEvent:Connect(function(plr) -- needed the plr argument since you fired a remote event print(1) Sword = plr.Backpack.Sword.Handle Sword.Touched:Connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") and not debounce then debounce = true print(2) Hit.Parent.Humanoid:TakeDamage(10) wait(0.5) debounce = false end print(3) end) print(4) end)