So I’m trying to make a crate system and when the player opens a crate then the event triggers and gives the player a random item. It’s also supposed to show the player the name of the item thru a cloned gui, but for some reason it gives me an error at line 11 and 17 saying:
ServerScriptService.RareCratePrize:17: attempt to index nil with ‘ItemImage’
(It will also say it at line 11 but that depends on the prize)
Here is the script in ServerScriptService:
local RS = game:GetService("ReplicatedStorage")
local unlockEvent = RS:FindFirstChild("CrateEvents").UnlockedBlade
unlockEvent.OnServerEvent:Connect(function(player)
local randomNumber = math.random(1,2)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 1000
if randomNumber == 1 then
player.OwnedItems.Arador.Value = player.OwnedItems.Arador.Value + 1
local gui = player.PlayerGui:FindFirstChild("RareCrateOpen")
local itemImage = gui.ItemImage
itemImage.ItemName.Text = "ARADOR"
elseif randomNumber == 2 then
player.OwnedItems.Blade.Value = player.OwnedItems.Arador.Value + 1
local gui = player.PlayerGui:FindFirstChild("RareCrateOpen")
local itemImage = gui.ItemImage
itemImage.ItemName.Text = "BLADE"
end
end)
Attempt to index nil with _ means that the code is attempting to index something that doesn’t exist. Check the path you created for the property you’re trying to find.
Also, the PlayerGui “RareCrateOpen” may not have loaded in when this script is running, so I recommend changing this line:
To this:
local gui = player.PlayerGui:WaitForChild(“RareCrateOpen”)
Make sure to do this on both lines where you create the ‘gui’ variable.
Okay thank you all for the answers, but I realized that I can just clone it thru the server script instead of the local script and it will fix the issue.
Changes the client makes to their PlayerGui (such as creating a gui, deleting a gui, or changing the text in a textbox) don’t replicate to the server, meaning the server is unable to see these changes.
Ideally, you should be using a remote function so the client can request for the random number, then the server will increment the value and return the random number to the client.
What you’re doing right now works fine, but it might be a bit laggier than using a remote function (as in the delay between unlocking the crate and having the text change would be slightly longer)