Error When Referencing LocalPlayer

Hello everyone,

Right now I’m facing an issue with my game, and i can’t find a solution to it since it doesn’t really make sense to me.

local Player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function(hit)
	hit.Parent.HumanoidRootPart.Position = game.Workspace.Restarter1.Position
	hit.Parent.Humanoid.WalkSpeed = 0
	Player.PlayerGui.WrongGui.WRONGScript.Disabled = false -- Error here
	wait(1)
	hit.Parent.Humanoid.WalkSpeed = 20
end)

Apparently, if you take a look at the script, you’ll notice that I’ve referenced the “LocalPlayer” in the first line of the code, and used that to get to the player’s Gui and Enable a script.
But for some reason, it stops at the 6th line and give’s me an error message in the output, as shown in the image:

image

I’ve tried writing down the code in different ways and even changed the script type and the parent, nothing seems to work.

I would appreciate some help if you can.
Thank You.

1 Like

You can only reference local player by a local script, are you sure it’s a local script?

Are you sure it’s a local script?

A local Script doesn’t trigger when i touch the part

Anyway the problem is you put a local script in workspace if u wanna get the player who touched the part here’s an example

Ps=game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
    local Player = Ps:GetPlayerFromCharacter(hit.Parent)
	hit.Parent.HumanoidRootPart.Position = game.Workspace.Restarter1.Position
	hit.Parent.Humanoid.WalkSpeed = 0
	Player.PlayerGui.WrongGui.WRONGScript.Disabled = false -- Error here
	wait(1)
	hit.Parent.Humanoid.WalkSpeed = 20
end)
1 Like

Hopefully this should help.



script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Added this

	print(player.Name)
	hit.Parent.HumanoidRootPart.Position = game.Workspace.Restarter1.Position
	hit.Parent.Humanoid.WalkSpeed = 0
	player.PlayerGui.WrongGui.WRONGScript.Disabled = false -- Error here
	wait(1)
	hit.Parent.Humanoid.WalkSpeed = 20
end)

You cannot get a LocalPlayer from a serverscript, so you need to get the player from the touched function.

1 Like

Local scripts do not work in workspace. use it in PlayerGui, StarterPlayerScripts, or ReplicatedFirst (or you can change it to a server script and do what iMonody_X did).

I’ve tried that before, but this is what happened…

apparently when you teleport the player’s HumanoidRootPart, the rest of the body teleport with it, but that doesn’t happen with a local script

set the CFrame and not the position. using position with a HumanoidRootPart glitches it out.

1 Like

I’ve tried your script and @iMonody_X script, both of them works, however, the line where it say’s “Player.PlayerGui.WrongGui.WRONGScript.Disabled = false” activate ONLY the first time. but then stop working.

Then Use

Player.PlayerGui.WrongGui.WRONGScript.Disabled = true

After

hit.Parent.Humanoid.WalkSpeed = 20

the “WRONGScript” does already have a script disable.
image

ok well, your method actually worked but isn’t there a way to make it work from the Workspace?

Yes change it into a normal script

That’s not what I meant, I wanna put the script inside the part that is supposed to be touched and trigger the event, regardless of the type of the script

Sorry I’m late, but no. There is no way to make Local Scripts work inside of workspace. Only normal scripts can.

1 Like

I know the topic is already solved, but just wanted to add that they do work in the workspace if parented to a player character :slight_smile:

1 Like

thx, this information will be useful