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

"Change" function with Torso CFrame coming up nil?

Asked by 5 years ago

I keep getting an error on the following code that says Torso is a nil value, which does not make sense to me. This is part of a time bomb weapon. When the bomb hits a player, I want it to float in front of them for a few second to build suspense before exploding. It sticks to the player's torso as intended, so my idea was that if the torso's CFrame was changed (the player moves) then the bomb would change to always be in front of the torso. Maybe there is an easier way to do this without running a "Changed" function over and over again, but I'm out of ideas. Let me know if you want to see the entire code.

local function OnTouched(otherPart)
    if otherPart.Parent:findFirstChild("Humanoid") then
    if Rocket and otherPart then
        Torso = otherPart.Parent:findFirstChild("Torso")
        dir = Torso.CFrame.lookVector
        pos = Torso.Position + (dir*3)
        Touched = true
    end 
    end
end


Torso.Changed:connect(function()
    game.Workspace.Baseplate:Destroy()
    if Touched == true then
        Rocket.CFrame = CFrame.new(pos, pos + dir)
    end
end)

Rocket.Touched:connect(OnTouched)
0
Why are you using findFirstChild User#19524 175 — 5y
0
Probably because what you're detecting is R15. R15 doesn't use Torso. Zenith_Lord 93 — 5y
0
Zenith_Lord - Thank you! I didn't realize that so I will look into it and try it with an R6 player. tygerupercut3 68 — 5y
0
change `findFirstChild` to `FindFirstChild` and `connect` to `Connect`. The former versions of both are deprecated. chomboghai 2044 — 5y
View all comments (6 more)
0
Also I just realized that Torso.Changed is a typo. I meant to say Torso.CFrame.Changed. tygerupercut3 68 — 5y
0
No, that would error. ^ User#19524 175 — 5y
0
I've fixed these things and it still says Torso is nil, which I know isn't true because if I add Torso:Destroy() then it destroys. I think Torso.CFrame.Changed is the problem so I'm going to find another method to do this. Thank you for telling me about the deprecated things, I've used the wrong one several times in other code. tygerupercut3 68 — 5y
0
I know this is probably bad but I use humanoid.Changed to check for character movement. Griffi0n 315 — 5y
0
I think there is a better way to detect player movement. humanoid.Running. saSlol2436 716 — 5y
0
Thank you to everyone who commented on this question, I found a tutorial on making a floating object on the Roblox wiki that I had not seen before. After reading that, I found that welds work way easier and I was making a simple task too complicated. I didn't realize welds could do that but it is working great so I'm just gonna stick with it. BodyPosition would also have a similar effect. tygerupercut3 68 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The problem is R15 players don't have a child 'Torso' but both have HumanoidRootPart which I recommend using now of days.

Ad

Answer this question