Still have questions? Join our Discord server and get real time help.
Log in to vote
0

# Player is a 'Nil Value' in a LocalScript ?

Asked by 2 years ago

So I'm trying to make a player TP onto a part once clicked on it, but I get this message:

19:14:06.480 - Workspace.Plate.Script:17: attempt to index upvalue 'Player' (a nil value)

I've already searched for help from here and other places, but they don't fix the issue I have, the script is down below:

local Plate = script.Parent
local Player = game.Players.LocalPlayer
Owned = false

function OnHover()
Plate.BrickColor =  BrickColor.Green()
Plate.Material = Enum.Material.Neon
end

function OnLeave()
Plate.BrickColor =  BrickColor.Gray()
Plate.Material = Enum.Material.SmoothPlastic
end

function OnClicked()
if Owned == false then
Player.Character:MoveTo(Plate.Position)
Owned = true
end
end

Plate.ClickDetector.MouseClick:connect(OnClicked)
Plate.ClickDetector.MouseHoverEnter:connect(OnHover)
Plate.ClickDetector.MouseHoverLeave:connect(OnLeave)


Hope anyone could help me with this...

0
This code isn't correct as clickdetector is server side but locaplayer is client side, also don't use moveto, change the CFrame on the HumanoidRootPart RedcommanderV2 1256 — 2y

### 2 answers

Log in to vote
0
Answered by 2 years ago

I believe that localPlayer only works in local scripts that are in the player scripts.

0
Also tried using a normal Script LordTechet 53 — 2y
0
And it is currently a LocalScript LordTechet 53 — 2y
0
yes but it needs to be in the player not in the part. If you are going to make it work you may have to make it all again. Also be sure its a local script. Magnetic_Aviator 21 — 2y
0
If you don't understand then its not even worth trying. Magnetic_Aviator 21 — 2y
View all comments (2 more)
0
I see what I must do, but now it gets harder as I must find the Plate I just clicked on LordTechet 53 — 2y
0
Yep. Thats what I said. That is the only way it will work. Magnetic_Aviator 21 — 2y
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

This solution utilizes the player variable of MouseClick. You just have to put it into the function as a parameter.

local Plate = script.Parent
Owned = false

function OnHover()
Plate.BrickColor =  BrickColor.Green()
Plate.Material = Enum.Material.Neon
end

function OnLeave()
Plate.BrickColor =  BrickColor.Gray()
Plate.Material = Enum.Material.SmoothPlastic
end

function OnClicked(parameterA) -- Receive player parameter
if Owned == false then
parameterA.Character:MoveTo(Plate.Position)
Owned = true
end
end

Plate.ClickDetector.MouseClick:connect(OnClicked(player)) -- Fire the function with the player parameter included.
Plate.ClickDetector.MouseHoverEnter:connect(OnHover)
Plate.ClickDetector.MouseHoverLeave:connect(OnLeave)


This is made for server scripts, by the way. I hope this solves your problem!

0
Pretty good answer, just edit the moveto to CFrame :) RedcommanderV2 1256 — 2y
0
I'm assuming that rotation is not really needed, as supersmartnoobguy put as Position. TdaGreat04 76 — 2y