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

My shop gui breaks swords once they clone to the player backpack. How can I fix this?

Asked by 4 years ago
Edited 4 years ago

I've tried to use a different storage and I've made sure the swords work without it. Its probably the cloning but I don't know what to change about it.

local ReqGold = 10 -- Purchase Amount Here
local AvGold = game.Players.LocalPlayer.leaderboard.Gold --This is the player leaderboard stat for gold
local Rs = game.ReplicatedStorage.swords
local IFS = Rs.BronzeSword
local BP = game.Players.LocalPlayer.Backpack

function Buy()
    if AvGold.Value >= ReqGold then
        AvGold.Value = math.abs(AvGold.Value - ReqGold)
        local copy = IFS:Clone()
        copy.Parent = BP
    end
end

script.Parent.MouseButton1Click:Connect(Buy)

`

1 answer

Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
4 years ago
Edited by User#24403 4 years ago

It may be because the swords also apply damage (and sometimes add creator tags) on the server-side. This means that since you are adding the sword from the client (using a LocalScript), on the server-side, the sword was never added to the player's backpack. You can make this work by using a RemoteEvent and firing it from the LocalScript, having the signal be received by a server script. You can put the RemoteEvent in the same location as the scripts, in ServerStorage, etc.

So the LocalScript should look something like this:

local ReqGold = 10
local AvGold = game.Players.LocalPlayer.leaderboard.Gold
local RemoteEvent = script.Parent.RemoteEvent --This is assuming that the RemoteEvent has the same Parent as this script. If you decide to move it elsewhere then replace this line.
local player = game:GetService("Players").LocalPlayer

function Buy()
    if AvGold.Value >= ReqGold then
        AvGold.Value = math.abs(AvGold.Value - ReqGold)
        RemoteEvent:FireServer()
    end
end

script.Parent.MouseButton1Click:Connect(Buy)

And the server script should look something like this:

local RS = game.ReplicatedStorage.swords
local IFS = RS.BronzeSword
local RemoteEvent = script.Parent.RemoteEvent --This is assuming that the RemoteEvent has the same Parent as this script. If you decide to move it elsewhere then replace this line.

RemoteEvent.OnServerEvent:Connect(function(player)
    IFS:Clone().Parent = player.Backpack --The other line is unnecessary as we do not need to create a variable for the copy of the sword if we are not using it later in the script.
end)

If there are any problems with these scripts, please let me know as I would be happy to help. Also, if you don't understand something about the scripts or my explanation please let me know so I can help you understand. Using code that you don't understand will never help you learn.

0
Thanks so much Wiggles1305 49 — 4y
0
You jerk, you should be accepting his answer if it helped, I'll just do it, you are missing out on +2 reputation points, now they're mine User#24403 69 — 4y
Ad

Answer this question