Feedback on my Buying script

 game.ReplicatedStorage.ToolEvents.SwordEvent:Connect(function(player)
     if player.leaderstats.CashValue>=100 then
     player.leaderstats.CashValue = player.leaderstats.CashValue - 100
   game.ServerStorage.Tools.Sword:Clone().Parent = player.Backpack
   end
 end)

Is this a good script ?

Where’s the event after connecting?

Its a very normal buying script
But you can do

player.leaderstats.CashValue -= 100

Instead of

player.leaderstats.CashValue = player.leaderstats.CashValue-100

Aslo
If you want the tool stays when respawning you should do this

game:GetService("ServerStorage").Tools.Sword:Clone().Parent = player.StarterGear

But in general is good in my opinion

1 Like

You need to connect a function to it, otherwise it will result in an error!

Replace:

with

game.ReplicatedStorage.ToolEvents.SwordEvent.OnClientEvent:Connect(function(player)
1 Like

But I already put the script into ServerScriptService

It doesn’t automatically connect. Use OnClientEvent like what @CommanderRanking said.

As I am not so much experienced in scripting I tried to script a very simple code to the shop…

Yes, however, you would still need an event for it, otherwise, it doesn’t have anything TO connect a function to!

1 Like

I would quickly advice to check out this article.

Also here is what the code should look like! If you are doing this ServerSided, which I suppose you should because you are connecting leader stats with it.

game.ReplicatedStorage.ToolEvents.SwordEvent.OnServerEvent:Connect(function(player)
     if player.leaderstats.Cash.Value >= 100 then
        player.leaderstats.Cash.Value -= 100
        game.ServerStorage.Tools.Sword:Clone().Parent = player.Backpack
     end
end)

Now remember, on the local side you would be putting.

game.ReplicatedStorage.ToolsEvent:FireServer()

Another thing I could point out is depending if you want the gear to stay with the player throughout the whole game instead of just when they respawn then…

game.ServerStorage.Tools.Sword:Clone().Parent = player.StarterGear

This would be more advised.

Hope this helps!!!

1 Like