Stop when touch problem

I am trying to make the player throw a ball on NPC, the ball should stop when it touches the NPC.
everything is working fine the first time but after that, the ball is not stopping but it touch and damage the NPC.
I can’t understand why it works just the first time.


This is the script in the ball:

local function onTouched(part)
	local playerCharacter = workspace:FindFirstChild(script.Parent.Owner.Value)
	if part:IsDescendantOf(script.Parent) or part:IsDescendantOf(playerCharacter) then return end
	
	if touched == false then
		local id = string.split(script.Parent.OwnerWeaponID.Value, "-")
		local firstCheck = part.Parent:FindFirstChild("Humanoid")
		local secondCheck = part.Parent.Parent:FindFirstChild("Humanoid")
		if firstCheck then
			if firstCheck:IsA("Humanoid") then
				if (game.Players:FindFirstChild(firstCheck.Parent.Name) == nil) then
					touched = true
					weaponFunc.damageNpc(firstCheck, playerCharacter.Name, id[2], 0.3)
					print("pat name: ",part.Name)
					script.Parent:MoveTo(script.Parent.PrimaryPart.Position)
					script.Parent.PrimaryPart.Velocity = Vector3.new(0,0,0)
					script.Parent.Ball.Trail.Enabled = false
					script.Parent.Ball.ParticleEmitter.Enabled = false
					print("stopped moving")
				end
			end
		else
			return
		end
	end
end
script.Parent.Ball.Touched:Connect(onTouched)

And that the function that fires the ball:

local function FireMagicBall(EndPos)
	local ball = game.ServerStorage.MagicStorage.MagicBall:Clone()
	ball.Owner.Value = playerCharacter.Name
	ball.OwnerWeaponID.Value = script.Parent.ID.Value
	local direction = CFrame.new(playerCharacter.HumanoidRootPart.Position, mouseHit).lookVector * 5
	ball.Parent = game.Workspace
	ball.PrimaryPart.CFrame = CFrame.new(playerCharacter.HumanoidRootPart.Position + direction)
	ball.PrimaryPart.Velocity = direction * 50
	ball.PrimaryPart:SetNetworkOwner(nil)
end
1 Like

i’m not expert at scripting but, try to put an :Destroy() when it touch the humanoid or something.

you set touched variable to true and that contradicts the if touched == false statement. Do you reset it back to false?

every time I fire a new ball so it’s a new script (I made this to make the ball damage just one NPC if it touches more than one), and the script prints “part name: …” and “stopped moving” every time
.

1 Like