Object not killing humanoid

Hi! So I made a script where when you step on a trigger part, it anchors your torso, and sends a spike down to kill you. (The spike has its own kill script inside) But I ran into an error. As the spike is moving down, it does not kill the player, but the script does work in other occasions. Here is the script:

local spike = game.ReplicatedStorage.Spike

function descendMovement()
	spike.Position = spike.Position - Vector3.new(0, 5, 0)
end

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		print(tostring(hit.Parent).. " touched the Part!")
		hit.Parent.Torso.Anchored = true
		wait(3)
		spike.Position = hit.Parent.Head.Position + Vector3.new(0, 200, 0)
		spike.Parent = game.Workspace
		while true do
			descendMovement()
		end
	end
end)

Try a break in the whiletrue do loop, Could you show us the spike script please?

function onTouched(hit)
	if not hit or not hit.Parent then return end
	local human = hit.Parent:findFirstChild("Humanoid")
	if human and human:IsA("Humanoid") then
		human:TakeDamage(1000)
	end
end

script.Parent.Touched:connect(onTouched)

When should I break the loop? Adding so i can pass limit

Put it under descendMovment()

e

It doesn’t work, the spike just remains in place once spawning in workspace. I meant like should I add a wait?

You should ALLWAYS add a wait in a while loop as it will often crash the game also i think it is why it doesn’t work because it may move so fast you can see it due to it

Yes, just add a wait() under descendMovement()

I added the wait, but still same thing.

Also you should add

if spike.Position.Y <= --[[height to stop at]]-- then
       break
end

inside the while loop

It stops, but doesn’t kill me.

Then it must be the spike that doesn’t work because if the spike touches you and doesn’t kill you it is the spike that is wrong so could you show the code for it?

It does, I tried it in another occasion where I unanchored the spike, and as it hit the floor and bounced up, it killed me. I posted the spike kill script up above

Could you try not to anchor the player and just let the spike hit you to see what happens?

It kills me, but I wanted it so that it would anchor me and kill me at once.

Maybe just set the players Humanoid.WalkSpeed and Humanoid.JumpPower to 0 if the anchoring part is causing issues? This will make it so they can’t move. You could also try anchoring the player’s torso.

1 Like

So i think the reason is that the player is achored but one solution would be to spawn an invisible box around the player. It won’t be the same effect but i think it might be the easiest.

Just so you know that’s what’s he is doing that doesn’t work

Maybe try anchoring the HumanoidRootPart instead? and try what @synical4 suggested.

Yeah, just realised that they were anchoring the torso not the humanoid root part… I suggest trying to anchor the hrp or messing with the humanoid.