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

Player is a 'Nil Value' in a LocalScript ?

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...

1 comment

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

2 answers

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

6 comments

0

Also tried using a normal Script

0

And it is currently a LocalScript

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.

0

If you don't understand then its not even worth trying.

0

I see what I must do, but now it gets harder as I must find the Plate I just clicked on

0

Yep. Thats what I said. That is the only way it will work.

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!

2 comments

0

Pretty good answer, just edit the moveto to CFrame :)

0

I'm assuming that rotation is not really needed, as supersmartnoobguy put as Position.

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