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

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

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

✓ Accepted answer

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

2 comments

0

Thank you AlmostADemon!

0

No problem!

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