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

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

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

✓ Accepted answer

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.

2 comments

0

Thanks so much

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

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