I really need help fixing this problem

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:

https://gyazo.com/41effd81a777d8d3886f0b4b4a4b0764

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 would love some help.

1 Like

Is the bolt welded to the crossbow?

Yeah, but it sticks to other even when its not equipped

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.

Okay, i will try that, i have never heard of that :Once thing

It basically disconnects from the event once it’s fired.

thats actually so coo l, i will test it rn

I noticed that it also activates when it touches parts of the crossbow, how can i prevent that?

copy.Touched:Once(function(hit)
	if not Stuck and hit.Parent == tool and hit.Parent.Name ~= owner.Name then
	Stuck = true

that didnt really work.
This is the hiarchy:

image

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.

i just use what i know, i have no idea how stuff using raycast would look like…
I dont have a big library full of knowledge as im still learning

I’ll just give you the refined code, here:


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)

Paste this in and it should work fine.

Thanks, but what did u add? I want to learn instead of always copying others

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.