Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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 :>

0
When a player respawns the torso is nil (for a few ms.). add "if torso then" on line 7 RubenKan 3615 — 7y
0
Thanks! That was a simple fix and it worked :) Heavening 10 — 7y

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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)

0
sure :) Heavening 10 — 7y
Ad

Answer this question