Hi, so my goal in this script is to give an user a tray after clicking this button on my GUI that I’ve made but it is not working and I have ensure that the tool is correctly named as “Tray” and also in the ServerStorage folder.
11:49:45.195 Infinite yield possible on 'ServerStorage:WaitForChild("Tray")' - Studio
11:49:45.195 Stack Begin - Studio
11:49:45.195 Script 'Players.Berkaygul9.PlayerGui.InteractionGUI.Frame.GetTrayButton.LocalScript', Line 14 - function getTrayModel - Studio - LocalScript:14
11:49:45.195 Script 'Players.Berkaygul9.PlayerGui.InteractionGUI.Frame.GetTrayButton.LocalScript', Line 33 - function giveTrayToPlayer - Studio - LocalScript:33
11:49:45.196 Script 'Players.Berkaygul9.PlayerGui.InteractionGUI.Frame.GetTrayButton.LocalScript', Line 56 - function onGetTrayButtonClicked - Studio - LocalScript:56
11:49:45.196 Stack End
Code:
-- LocalScript inside the GetTrayButton (Verify this again)
local function getTrayModel()
local serverStorage = game:GetService("ServerStorage")
--Check Server Storage exists.
if not serverStorage then
warn("ServerStorage service not found!")
return nil
end
--Quick wait check (testing).
wait (1)
local trayModel = serverStorage:WaitForChild("Tray") -- Set a timeout here.
return trayModel
end
-- Function to give the player a tray
local function giveTrayToPlayer(player)
-- Find the player's character and humanoid
local character = player.Character or player.CharacterAdded:Wait()[1]
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
warn("Humanoid not found for player:", player.Name)
return
end
-- Get the Tray MeshPart
local originalTray = getTrayModel()
--Check if original tray actually exists.
if originalTray == nil then
warn("Tray Model returned as nil.")
return
end
-- Clone the Tray MeshPart
local tray = originalTray:Clone()
-- Parent the tray to the player's backpack
tray.Parent = player.Backpack
print("Gave tray to player:", player.Name)
end
--Button function (Get Tray)
local function onGetTrayButtonClicked() -- Corrected: No player argument here
-- Get the local player
local player = game.Players.LocalPlayer -- Get Player in event handler!
--Gives the play the tray.
giveTrayToPlayer(player)
--Once they have a tray it will close.
script.Parent.Parent.Visible = false
end
-- Setup the button
local getTrayButton = script.Parent
-- Check if the button exists before connecting the event
if getTrayButton then
getTrayButton.MouseButton1Click:Connect(onGetTrayButtonClicked)
else
warn("GetTrayButton not found in Frame!")
end