'Cloning Locally and not locally [Lua]

I've been trying to make a sword fight game on Roblox studio. I've made a shop Gui so you can click the text button to buy the sword. It works well, You click it checks your kills and if you have enough you get the weapon. In this case, it's 0 kills. But when you take out the sword you cant use it. I've done my research and I believe that's because it's been cloned locally and not globally. Is this the case and if so how do I fix it?

The script in the Text Button:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    if player.leaderstats.Kills.Value >= 0 then
        local clonar = game.ServerStorage.ClassicSword:Clone()
        clonar.Parent = player.Backpack
    end
end)

Thanks in advance!



Solution 1:[1]

When work needs to be performed on the server (as opposed to locally on the client), you can use RemoteEvents to communicate across the client-server boundary.

So first, you'll need to create a RemoteEvent in a shared location, like ReplicatedStorage.

Next, update your client-side LocalScript to fire the RemoteEvent :

local player = game.Players.LocalPlayer
local buyEvent = game.ReplicatedStorage.RemoteEvent

script.Parent.MouseButton1Click:Connect( function()
    buyEvent:FireServer()
end)

Finally, you need to create a Script in the Workspace or ServerScriptService that listens for that RemoteEvent and performs the work of giving the player the item :

local buyEvent = game.ReplicatedStorage.RemoteEvent

buyEvent.OnServerEvent:Connect( function(player)
    if player.leaderstats.Kills.Value >= 0 then
        local clonar = game.ServerStorage.ClassicSword:Clone()
        clonar.Parent = player.Backpack
    end
end)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kylaaa