GOAL: A player touches a certain part to get a tool from a UI (activates when player touches part). Then they make their way to another location to change their tool (another UI), by removing the tool from their backpack and giving them a new one from ReplicatedStorage.
--Player touches a part, UI tool giver pop up and closes when player picks a tool. Player moves to touch another part to have another UI pop up. This new UI allows them to remove their old tool, giving them a new tool.
ISSUE: All works perfectly, the problem is when a player has a tool equipped when they touch the part, the UI breaks. It neither gives a tool nor closes.
Shows UI on Touch -script
local debounce = false
function getPlayer(humanoid)
local players = game.Players:children()
for i = 1, #players do
if players[i].Character.Humanoid == humanoid then return players[i] end
end
return nil
end
function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
if (human ~= nil) and debounce == false then
debounce = true
local player = getPlayer(human)
if (player == nil) then return end
script.Parent:clone().Parent = player.PlayerGui
wait(5)
debounce = false
wait()
end
end
script.Parent.Parent.Touched:connect(onTouch)
Tool Giver GUI -local script
local button = script.Parent
local tool = game.ReplicatedStorage:WaitForChild("Mug")
local plr = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
tool:Clone().Parent = plr.Backpack
script.Parent.Parent.Parent.Enabled = false
end)
Takes and Replaces Tools -local script
local button = script.Parent
local tool = game.Players.LocalPlayer.Backpack.Mug
local plr = game.Players.LocalPlayer
local new = game.ReplicatedStorage.Water
button.MouseButton1Click:Connect(function()
tool:Destroy()
new:Clone().Parent = game.Players.LocalPlayer.Backpack
script.Parent.Parent.Parent.Enabled = false
end)
CONCLUSION: How do I make it so the UI Tool Giver does NOT break when a player approaches with a tool equipped.

1 comment
The problem is that you didnt use a remote event since the local player cannot give itself a tool.