Why do i get this error:
Players.emay0660.PlayerGui.CollectableUI:9: attempt to index local 'player' (a nil value)
this script is in my StarterGui folder. Its not local script.
Here my code piece:
local player = game.Players.LocalPlayer
local keyGui = player.PlayerGui:WaitForChild("keyGui")

2 comments
"it's not local script" is the reason that player is nil. LocalPlayer is strictly client-only, meaning normal Scripts cannot use it, even if you tried to replicate it to the server. Also, as an additional point, the server cannot access PlayerGui's descendants, but if it were to assign something to PlayerGui, it can access what it put in there.
should be a localscript like bash said since normal scripts don't know what the local player is. localscripts should be used for guis / anything you want to show for your player only, not everyone else in the game