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

Script does not work after player resets?

Asked by 2 years ago

So, I have made a script that makes the player's character face the cursor, but when I reset, the script just stops working. I have looked all over the internet (even on page 2 of Google) but I have not found anything that fixes this problem. Any help is appreciated!

(For those who were wondering, when I said reset I meant my character.)

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local root = char:WaitForChild("HumanoidRootPart")
local attatchment = Instance.new("Attachment",root)

local orientation = Instance.new("AlignOrientation",root)
orientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
orientation.MaxTorque = math.huge
orientation.Responsiveness = 200
orientation.Attachment0 = attatchment

game["Run Service"].RenderStepped:Connect(function()
    if mouse.Hit and char.Humanoid.Health ~= 0 then
        root:FindFirstChildOfClass("AlignOrientation").CFrame = CFrame.new(root.Position,Vector3.new(mouse.Hit.Position.X,root.Position.Y,mouse.Hit.Position.Z))
    end
end)

0
When I run the code, the only error it gives me is the plr.character is trying to index nil. I might have a solution but it's take a bit. VipDanT 10 — 2y
0
vipDan, you are tryna use it in a serverscript when it has localplayer... :skull: Kingu_Criminal 205 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

The reason it isn't working is because you only do it once. When they die, you don't create the attachment again. Replace your current code with this:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

function create(root,char)
    local attatchment = Instance.new("Attachment",root)

    local orientation = Instance.new("AlignOrientation",root)
    orientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
    orientation.MaxTorque = math.huge
    orientation.Responsiveness = 200
    orientation.Attachment0 = attatchment

    game["Run Service"].RenderStepped:Connect(function()
        if mouse.Hit and char.Humanoid.Health ~= 0 then
            root:FindFirstChildOfClass("AlignOrientation").CFrame = CFrame.new(root.Position,Vector3.new(mouse.Hit.Position.X,root.Position.Y,mouse.Hit.Position.Z))
        end
    end)
end

plr.CharacterAdded:Connect(function(char)
    local root = char:WaitForChild("HumanoidRootPart")
    create(root,char)
end)

Hope this helps!

0
Thanks dude! JaydenLegenderyDude 22 — 2y
Ad

Answer this question