Why is my script not working

My script is supposed to check if a player has enough money to see a GUI

local player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function()
	if player.Data.Levels.Value == 1 then
		game.StarterGui.ScreenGui.Frame.Visible = true
	end
	
	if player.Data.Levels.Value == 2 then
		game.StarterGui.ScreenGui.Frame.Visible = true
	end
end)

The output says

 Workspace.SpawnLocation.Script:4: attempt to index nil with 'Data'

Is this a server script. If so you can’t use LocalPlayer, that’s only available in local scripts.

What you can do to fix this is

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then

It is a normal script in a part

So it’s a server side script? Put this in a local script or do what above poster says.

OK then yes it can’t access LocalPlayer, that’s only available for local scripts, the fix is what I gave

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
	if player.Data.Levels.Value == 1 then
		player.PlayerGui.ScreenGui.Frame.Visible = true
	end
	
	if player.Data.Levels.Value == 2 then
		player.PlayerGui.ScreenGui.Frame.Visible = true
	end
end
end)
1 Like

With this, you are referencing the StarterGui instead of the player’s PlayerGui.

You should change it to

player.PlayerGui.ScreenGui.Frame.Visible = true
1 Like

This is incorrect. You need to change the PlayerGui, not the ScreenGui. It’s recommended to do this on the client.

12 Likes

Yes, sorry I just copied/pasted OPs code with that in, I will correct.
It can be done on the server side though, does not have to be local script

But either way, i’d guess that when the buttons on these frames are pressed, would want some additonal server side checks again to make sure the player really does have enough money.

I’d recommend firing a remote event. To interact with Part server script and local player GUI

It does not work image It says this in the output

ServerScriptService.Script:4: attempt to index nil with 'Data'
1 Like

You cannont access local player from a Server Script, Server Scripts are ran by Roblox Server and not your machine (Local Player) So when you get the touched event, fire a remote event, put a local script inside StarterGUI to catch this remote event and then bind your actions

Could you post the updated script here and we can look and try to help

Hi okay now I fixed the script thank you for every who replied

2 Likes