How do u fix this error code?

anyone know how to fix this?

Workspace.KeyboardActivator.Script:5: attempt to index nil with 'WaitForChild' 
local touch = script.Parent

local player = game.Players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Cash")

if Money.Value > 30 then
	touch.Touched:Connect(function()
		for i,part in pairs(game.Workspace.Keyboard:GetChildren()) do
			part.Transparency = 0
			part.CanCollide = true
		end

		touch.Transparency = 1
		touch.Highlight.Enabled = false
		touch.PointLight.Enabled = false
		touch.BillboardGui.Enabled = false
		touch.Sound:Play()
		touch.CanTouch = false
	end)
else
	print("Player has no more money to purchase Keyboard")
end



1 Like

Show the code please, we can’t diagnose the error without seeing the script

1 Like

you need to give the code if you want any help

2 Likes

You’re running this code from what I believe to be a server script. .LocalPlayer can only be used from local scripts.

Either that or you need to initialise the Players service first before calling .LocalPlayer.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")
2 Likes

‘leaderstats’ is not part of the player yet, have u made sure the leaderstats folder is created and parented under the player?

1 Like

yep
image
i made a script in serverscriptservice

1 Like

is the current script a local script or server script

1 Like

ohh its a normal script should i change it???

1 Like

thats where the problem is, u cant use players.LocalPlayer in a normal script, however you can do:

2 Likes

Im really stuck because right now im just tring to get it so that if you touch the part, and you dont have enough money, it just says “u dont have money” and not run that function. but incase the player does have enough money, it does

1 Like

You can change the positionings to this

1 Like

idk if im doing anything wrong but its not working. here is my new script

local touch = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Cash")

touch.Touched:Connect(function(hit)
	local plr = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)
	if plr then
		local Money = plr.leaderstats.Cash
		if Money and Money.Value >= 30 then
			for i,part in pairs(game.Workspace.Keyboard:GetChildren()) do
				part.Transparency = 0
				part.CanCollide = true
				
			end

			touch.Transparency = 1
			touch.Highlight.Enabled = false
			touch.PointLight.Enabled = false
			touch.BillboardGui.Enabled = false
			touch.Sound:Play()
			touch.CanTouch = false
		end
	end
end)


1 Like

change to just this new code ////////

1 Like

Didnt work, i get that ur prob pissed at me rn so imma just work on a different thing and get back to this later. Thanks

1 Like

I was scripting in the website so there may have been some error, this is my fix in studio:

local touch = script.Parent

touch.Touched:Connect(function(hit)
	local plr = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)
	if plr then
		local Money = plr.leaderstats.Cash
		if Money and Money.Value >= 30 then
			for i,part in pairs(game.Workspace.Keyboard:GetChildren()) do
				part.Transparency = 0
				part.CanCollide = true

			end

			touch.Transparency = 1
			touch.Highlight.Enabled = false
			touch.PointLight.Enabled = false
			touch.BillboardGui.Enabled = false
			touch.Sound:Play()
			touch.CanTouch = false
		end
	end
end)
1 Like

Still didnt work, do you want access to my game and try an fix it there?

1 Like

alright /////////////////////////

1 Like

okay ill friend u and let u access it

1 Like

do u have Discord? i rather communicate there

1 Like

yeah, heres my discord
flyca_23789

1 Like