I need help with an ordering system

Hi! I’m working on an ordering system and found this error when scripting it.

Players.TSF_Lacker.PlayerGui.Screen.FoodChoices.ScrollingFrame.FoodChoice2.OrderChange:5: attempt to index nil with ‘WaitForChild’

here is the code that is erroring

local OrderName = script.Parent.Text
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	player:WaitForChild("OrderVal").Value = OrderName
end)
1 Like

MouseButton1Click doesn’t return anything, so you won’t expect it to return a player. Also, the variable OrderName will just stay constant. I assume this is a normal script, so you’d do:

local player = script.Parent.Parent.Parent.Parent.Parent.Parent
player:WaitForChild("OrderVal").Value = script.Parent.Text

This is in the event, btw

For this script, i know its inefficient, but i have it under each of the food choice UIs, so that when the click it, OrderVal == the name of the UI

As said by the post above, MouseButton1Click does not return a player object. It doesn’t need to as there is one instance of the local script (and GUI) per player and you can get that player object when the script is initialised:

local player = game.Players.LocalPlayer
local OrderName = script.Parent.Text

script.Parent.MouseButton1Click:Connect(function()
	player:WaitForChild("OrderVal").Value = OrderName
end)

so i just have to create the variable player?

Yes, grab the local player at the start and then you can refer to it in all your functions.
If you replace your current code with the one I posted it should work as you intend.

Yes, Because MouseButton1Click doesn’t return the player

ok Thanks for the help! (30 chars)

I can see from the scripts full name that the script is a local script affecting a button, in the Player UI using local scripts player is not an argument - you are likely confusing this with BillboardGui and SurfaceGui or even click detectors which rely on scripts within the workspace to operate (mainly).

As such to get the player you can just do game.Players.LocalPlayer in the local script for the same result.

this is a server script, not a local script, and its still erroring after i added the vairiable player

yeah that player variable is specificlly made for local scripts

1 Like

so should i change the scripts to local?

A script… In the local players UI…

Are you sure your not confusing the UI giver script with the UI handler local script?

Scripts don’t run if they aren’t a descendant of the workspace or ServerScriptService.

the reference for the script is this

game.StarterGui.Screen.FoodChoices.ScrollingFrame.FoodChoice1.Script

Yes. You are detecting a button press which is done locally on the user’s client.

Any scripts inside GUI should be local such as ones dealing with button presses.

If you have scripts in Workspace/ServerScriptService, these should generally be server scripts.

thanks mate, ive only gotten serious about scripting this year and I’m still confused about a lot of things

1 Like

You can’t use Scripts on server, please change it into a Local Script.