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

How to Fix Double Cloning to Backpack Problem?

so I'm trying to make a ButtonGui that when pressed will give a gun (clone) to the LocalPlayer but, the problem is that it cloned 2 times (i think) and ended up like this (2 of the same gun in the backpack) https://gyazo.com/23740e9dd6dec07ab059052eaefc4cd5

these are my scripts

--server script
local famas = game.ReplicatedStorage.FAMAS
local scarL = game.ReplicatedStorage["SCAR-L"]
local remote = Instance.new("RemoteEvent")
remote.Parent = game.ReplicatedStorage
remote.Name = "WeaponEvent"
local active = game.StarterGui.Activated.Value
local playerName;
print("weaponscript test")

game.Players.PlayerAdded:Connect(function(player)
	playerName = player.name
end)
remote.OnServerEvent:Connect(function()
	wait(1)
	print(playerName .. " Is name")
	local famasClone = famas:Clone()
    famasClone.Parent = game.Players[playerName].Backpack
	active = false
	print("weaponscript activated")
end)

.

--Local script

local button = script.Parent
local weaponRemote = game.ReplicatedStorage:WaitForChild("WeaponEvent")

button.MouseButton1Click:Connect(function()
	weaponRemote:FireServer()
end)

can anyone help?

2 comments

0

Use FindFirstChild to check if the player does not have a weapon. If they don’t, then clone and parent the weapon to their backpack.

0

Don't forget to accept my answer if it solves your problem

1 answer

✓ Accepted answer

You're not checking if they already have the tool or not. You should check that before doing anything.

--server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local famas = ReplicatedStorage.FAMAS
local scarL = ReplicatedStorage["SCAR-L"]
local remote = Instance.new("RemoteEvent")
remote.Parent = ReplicatedStorage
remote.Name = "WeaponEvent"

-- # you don't need the PlayerAdded event listener here. 
remote.OnServerEvent:Connect(function(client)
    -- # client is the client that fired this remote event
    if not client.Backpack:FindFirstChild("FAMAS") and not client.Character:FindFirstChild("FAMAS") then
        famas:Clone().Parent = client.Backpack
    end
end)

Notice the additional check made in the character. When a tool is equipped its parented out of the backpack into the character. So it's not in backpack if it's equipped.

1 comment

0

You should also mention that you're using the automatically passed reference to the player in your function connected to the OnServerEvent signal in your answer.

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