Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI button to add a tool to a player Backpack and Starter gear not working how to fix?

I have made a gui button that when press with add a tool to the players Backpack and starter gear but it only goes into the backpack how do I fix this?

local p = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect (function()
	local tool = script.Parent.Sword
	tool:clone().Parent = p.Backpack
	tool:clone().Parent = p.StarterGear
end)

1 answer

✓ Accepted answer

How to make this

You need to use RemoteEvents to Replicate tool to Player, also the tool can't be in GUI, put in ReplicatedStorage in a folder of tools...

How to use RemoteEvents

-- Server(Script > ServerScriptService)
local RemoteEvent = ... -- Your RemoteEvent location (put in ReplicatedStorage the RemoteEvent)

-- The first argument of remote event, is player. In client you dont need to send player, but in Server the first argument need to be player, the other arguments is arguments sent by client
RemoteEvent.OnServerEvent:Connect(function(player,argument)
	if argument == "CreatePart" then -- Check if argument sent is equal to "CreatePart"
		Instance.new("Part").Parent = workspace -- Create a Part in workspace
	end
end)

-- Client(LocalScript > StarterGui,StarterPlayerScript,StarterCharacterScript and more...)
local RemoteEvent = ... -- Your RemoteEvent location (put in ReplicatedStorage the RemoteEvent)

-- script.Parent = TextButton
script.Parent.MouseButton1Click:Connect(function() -- On click start a function, also you can use script.Parent.Actived, but here i will use script.Parent.MouseButton1Click
	RemoteEvent:FireServer("CreatePart") -- Fire server with argument Create part, here you dont need to send player
end)

RECOMMENDATION

Stop with using :connect this is deprecated, for new work use :Connect also, stop with using :clone too, use :Clone

FIXED SCRIPT

Create a RemoteEvent in ReplicatedStorage, also create a Folder named as "Tools" in this folder put all tools

-- Server(Script > ServerScriptService)
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent location (put in ReplicatedStorage the RemoteEvent)
local tools = game.ReplicatedStorage.Tools -- Tools folder

RemoteEvent.OnServerEvent:Connect(function(player,toolName) -- Get player and toolName
	local Backpack = player:FindFirstChild("Backpack")
	local StarterGear = player:FindFirstChild("StarterGear")
	if Backpack and StarterGear then -- If find Backpack and StarterGear
		local tool = tools:FindFirstChild(toolName)
		if tool then -- If find the tool
			tool:Clone().Parent = Backpack -- Clone tool to Backpack
			tool:Clone().Parent = StarterGear -- Clone tool to StarterGear
		end
	end
end)

-- Client(LocalScript)
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent location (put in ReplicatedStorage the RemoteEvent)

script.Parent.MouseButton1Click:Connect(function() -- On click in mouse
	RemoteEvent:FireServer("Sword") -- Fire remote event sending the tool name
enn)

Hope it helped :)

2 comments

0

Thanks this helped a lot.

0

Also do you know if there is away to make it delete the old tools in the backpack and Startergear and then copy the new once over?

This discussion is preserved as it was. New answers, comments, and votes are closed.