Hi people, im making a crossbow tool and want to make it so the bolt (Arrow) stays in side player, so far so good. Its working as expected but when the bolt is in the player other players can interract with the bolt, see vid down below for more:
I really need help as im totally lost at how i can fix this. Not only that, the owner of the tool can shoot himself, i dont want that and added a check thats also not working, the script is down below:
local tool = script.Parent
local fireSpeed = 120
local owner = tool.Parent
tool.RemoteEvent.OnServerEvent:Connect(function(plr: Player, mousePos, Target)
local direction = (mousePos - tool.Start.Position).Unit
local bolt = game.ReplicatedStorage:WaitForChild("Bolt")
local copy = bolt:Clone()
copy.Parent = workspace
copy.CFrame = CFrame.new(tool.Start.Position, mousePos)
copy.Velocity = direction * fireSpeed
local Stuck = false
copy.Touched:Connect(function(hit)
if not Stuck and hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Parent.Name ~= owner.Name then
Stuck = true
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(22)
local weld = Instance.new("WeldConstraint", hit.Parent)
weld.Part0 = hit
weld.Part1 = copy
copy.Parent = hit.Parent
end
task.wait(1)
Stuck = false
end
end)
end)
I see the problem here, even after the bolt hits a character it still keeps listening for touched event. You should use copy.Touched:Once instead of copy.Touched:Connect.
Yeah, as @CEO_OfTrolling said you should use copy.Touched:Once. I would recommend for you to switch from Touched event to a simple Raycast as they are WAY better at detecting stuff. Touched events like to not detect stuff when they should and are just a hassle to work with.
local fireSpeed = 120
local owner = tool.Parent
local connection
tool.RemoteEvent.OnServerEvent:Connect(function(plr: Player, mousePos, Target)
local direction = (mousePos - tool.Start.Position).Unit
local bolt = game.ReplicatedStorage:WaitForChild("Bolt")
local copy = bolt:Clone()
copy.Parent = workspace
copy.CFrame = CFrame.new(tool.Start.Position, mousePos)
copy.Velocity = direction * fireSpeed
connection = copy.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Parent.Name ~= owner.Name and not hit:IsDescendantOf(script.Parent) then
connection:Disconnect()
humanoid:TakeDamage(22)
local weld = Instance.new("WeldConstraint", hit.Parent)
weld.Part0 = hit
weld.Part1 = copy
copy.Parent = hit.Parent
task.wait(1)
end
end)
end)
I removed the if humanoid then statement because you already check that in the first if statement, and I made it so that it disconnects from the event when the bolt hits a character.
Also, as @Artex_112 mentioned, you should use something like FastCast Redux because the touched event is very inaccurate and unreliable.