Give Tray GUI for my game

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a Give Tray GUI

  2. What is the issue?
    When I click on what tray I want it does this that on the video

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried adding :Destroy instead of :Remove in it still doesn’t work Right

-- local tool = game.Workspace:WaitForChild("CupcakeTray")
local player = game.Players.LocalPlayer
local cookie = script.Parent

cookie.MouseButton1Click:Connect(function()
	if player.Character then
		for _, item in pairs(player.Character:GetChildren()) do
			if item.Name == tool.Name then
				item:Destroy()
			end
		end
	end

	if player.Backpack then
		for _, item in pairs(player.Backpack:GetChildren()) do
			if item.Name == tool.Name then
				item:Destroy()
			end
		end
	end

	local clonedTool = tool:Clone()
	clonedTool.Parent = player.Backpack
end)

Is this local script or server script?

This part should be a local script and for the rest of your script it should be on the server side. You’ll need to use remote event to achieve this.

it is a local script for my GUI

Alr, You would need to use a remote event.
Create a remote event to Replicated Storage rename it to HandleCupcakeTray

Here is the local script

local tool = game.Workspace:WaitForChild("CupcakeTray")
local player = game.Players.LocalPlayer
local cookie = script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local handleCupcakeTrayEvent = ReplicatedStorage:WaitForChild("HandleCupcakeTray")

cookie.MouseButton1Click:Connect(function()
    -- Fire the RemoteEvent to the server
    handleCupcakeTrayEvent:FireServer()
end)

This is a server script place in serverscriptservice

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local handleCupcakeTrayEvent = ReplicatedStorage:WaitForChild("HandleCupcakeTray")

handleCupcakeTrayEvent.OnServerEvent:Connect(function(player)
    local tool = game.Workspace:WaitForChild("CupcakeTray")

    -- Check and destroy existing tools in the character
    if player.Character then
        for _, item in pairs(player.Character:GetChildren()) do
            if item.Name == tool.Name then
                item:Destroy()
            end
        end
    end

    -- Check and destroy existing tools in the backpack
    if player.Backpack then
        for _, item in pairs(player.Backpack:GetChildren()) do
            if item.Name == tool.Name then
                item:Destroy()
            end
        end
    end

    -- Clone the tool and add it to the player's backpack
    local clonedTool = tool:Clone()
    clonedTool.Parent = player.Backpack
end)

You may need to edit this to work with your system.

You might want to check that there are no anchored parts in your Tool, otherwise the character will be teleported to the position of the tool.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.