Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why aren't tools cloning from ReplicatedStorage properly?

Asked by
Memotag 226 Moderation Voter
6 years ago

The idea of my script is to click an on screen GUI and it will put a weapon inside the players Backpack and then the GUI will remove itself. The code is inside of a Localscript and it works perfectly when I test it in 'Play' mode but in 'Online' Mode the sword will appear in the users inventory but will make no sounds and you're unable to swing the sword itself.

Here's the code:

wait(1)
local player = game.Players.LocalPlayer
local sw = game.ReplicatedStorage.ClassicSword

function onButtonClicked()
        if player:FindFirstChild("Backpack", 10) then
            sw:Clone().Parent = player.Backpack
        wait(0.1)
        script.Parent:remove()
    end
end

script.Parent.MouseButton1Down:connect(onButtonClicked)

Any help will be appreciated to be able to stop the models from seemingly randomly becoming broken!

0
First of all, use Destroy(), remove() is deprecated. connect is also deprecated, use Connect insead. I don't think the problem lies in the script, but in the sword itself. It may have something to do with FilteringEnabled. I suggest using a different sword to see if that works. If it does, then it is a problem with the sword and not the script. lunatic5 409 — 6y
0
I've tried a large range of swords but they all seem to experience the same problem after being cloned. They work perfectly when put in the Starterpack but break when cloned and awarded to the player. Memotag 226 — 6y
0
I think you’ll need to add a script to ServerScripts and a RemoteEvent in ReplicatedStorage along with the weapon that will get cloned to the backpack ABK2017 406 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The problem is occurring on line 6. The second argument to FindFirstChild is a boolean, not a number. If this argument is set to true, it will look through the descendants of the object. Also, the MouseButton1Down event runs code whilst the mouse is down on the Gui, so it may repeat the code. If you do not want this behaviour, use MouseButton1Click.

wait(1)
local player = game.Players.LocalPlayer
local sw = game.ReplicatedStorage.ClassicSword

local function onButtonClicked()
    if player:FindFirstChild("Backpack") then
        sw:Clone().Parent = player.Backpack
        wait(0.1)
        script.Parent:Destroy()
    end
end

script.Parent.MouseButton1Click:Connect(onButtonClicked)

-- you may want the server to do the cloning so it replicates :\

On a side note, :remove() and :connect() are deprecated, use :Destroy() and :Connect() instead.

Ad

Answer this question