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

How can I fix up my script so it works in the actual game and not just in roblox studio?

Asked by 4 years ago
Edited 4 years ago

The script is supposed to make the part face the player, I tested it out in studio and it worked fine, but when I tested it ingame, the script doesnt work.

function MakeRightSurfaceFace(Pos, LookAt)
    local Up = Vector3.new(0,1,0);
    local LookAtDirection = (LookAt - Pos).unit

    if (LookAtDirection.Y > 0.98) then
        Up = Vector3.new(0,1,0);
    end

local ZAxis = LookAtDirection:Cross(Up);
return CFrame.new(Pos.X,Pos.Y,Pos.Z,LookAtDirection.X,Up.X,ZAxis.X,LookAtDirection.Y,Up.Y,ZAxis.Y,LookAtDirection.Z,Up.Z,ZAxis.Z);
end

while wait() do
    getPlayers = game.Players:GetChildren()
    for i = 1, #getPlayers do
        characters = getPlayers[i].Character.Torso
        if characters ~= nil then
            local magni = (script.Parent.Position - characters.Position).magnitude
            if magni < 25 then
                script.Parent.CFrame = MakeRightSurfaceFace(script.Parent.Position, characters.Position);
            end
        end
    end
end






All help is appreciated!

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The original code had an error where it couldn't find the character in the game, so I changed the way it looks for the character.

I made sure that it works in studio and in game since you originally had a problem with that.

Here is the code:

function MakeRightSurfaceFace(Pos, LookAt)
    local Up = Vector3.new(0,1,0);
    local LookAtDirection = (LookAt - Pos).unit

    if (LookAtDirection.Y > 0.98) then
        Up = Vector3.new(0,1,0);
    end

    local ZAxis = LookAtDirection:Cross(Up);

    return CFrame.new(Pos.X,Pos.Y,Pos.Z,LookAtDirection.X,Up.X,ZAxis.X,LookAtDirection.Y,Up.Y,ZAxis.Y,LookAtDirection.Z,Up.Z,ZAxis.Z);
end


while wait() do
    getPlayers = game.Workspace:GetChildren() -- Find all children inside the workspace
    for i = 1, #getPlayers do
        getCharacter = getPlayers[i] -- Get character from the player
        getPlayer = game.Players:FindFirstChild(getCharacter.Name)
        if getPlayer then -- If the target is a player
            torso = getCharacter:FindFirstChild("Torso")
            local magni = (script.Parent.Position - torso.Position).magnitude
            if magni < 25 then
                script.Parent.CFrame = MakeRightSurfaceFace(script.Parent.Position, torso.Position);
            end
        end
    end
end
0
Thank you AlmostADemon! ErskinePierre 48 — 4y
0
No problem! AlmostADemon 50 — 4y
Ad

Answer this question