Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My shark following script only works the first time, whats wrong?

So everything works fine the first time, the shark follows me and kills me. But the second time it says that the torso on Line 6-7 is nil and the shark just freezes in place :(

local bodypos = Instance.new("BodyPosition", script.Parent)
while true do
	wait()
	for i, v in pairs(game.Workspace:GetChildren())do
		if v:IsA("Model") and v:FindFirstChild("Humanoid") then
		local torso = v:FindFirstChild("Torso")
		magnitude = (torso.Position - script.Parent.Position).magnitude
		if magnitude < 500 then
			--script.Parent.Rotation = torso.Rotation
			bodypos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodypos.Position = torso.Position
			--bodypos.P = 10000
			--bodypos.D = 100000
			end
		end
	end
end

Help will be really appreciated :>

2 comments

0

When a player respawns the torso is nil (for a few ms.). add "if torso then" on line 7

0

Thanks! That was a simple fix and it worked :)

1 answer

✓ Accepted answer

Since my comment solved the problem, would you mind accepting this answer? Cheers ;D

(Comment: When a player respawns the torso is nil (for a few ms.). add "if torso then" on line 7)

1 comment

0

sure :)

This discussion is preserved as it was. New answers, comments, and votes are closed.