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

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

Asked by 7 years ago

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

Log in to vote
5
Answered by 7 years ago

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.

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) OldPalHappy 1477 — 7y
Ad

Answer this question