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

How can I get a "LocalPlayer", but in a normal script?

code:

function onClicked()
	game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(26.2, -10.855, 319.8))
		game.Workspace.Sounds.DinkSecretRoom:Play()
	end
	


script.Parent.ClickDetector.MouseClick:connect(onClicked)


THIS IS IN A NORMAL SCRIPT, IF ITS IN A LOCALSCRIPT IT BREAKS. PLEASE HELP ME. I NEED TO CALL A LOCALPLAYER BUT WITHOUT LOCALPLAYER. SORRY FOR CAPS I WAS TO LAZY TO TYPE AGAIN.

1 answer

While LocalPlayer can only be accessed through a Local Script, in your case, you could simply use the Player value of the ClickDetector object. Here's a better version of your code:

function onClicked(player)
    player.Character.HumanoidRootPart.CFrame = CFrame.new(26.2, -10.855, 319.8)
    game.Workspace.Sounds.DinkSecretRoom:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Also do note that CFrame.new doesn't require Vector3.new to be inside it.

1 comment

0

(Worth noting it's better to use local functions, just like it is with Local Variables, and :connect is deprecated in favor of :Connect)

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