Hi I am trying to make player pay Money to unlock a certain team. local script in Gui button
01 | script.Parent.MouseButton 1 Click:Connect( function () |
02 |
03 | local leaderstats = Instance.new( "Folder" ) |
04 |
05 | local sp = leaderstats:findFirstChild( "Money" ) |
06 |
07 | if sp = = nil then return false end |
08 |
09 | if (sp.Value > = 100 ) then --Amount of how much the weapon will go for |
10 |
11 | sp.Value = sp.Value - 100 |
12 |
13 | local team = script.Parent.Name |
14 |
15 | game.ReplicatedStorage.ChangedTeam:FireServer(team) |
16 |
17 | end |
18 |
19 | end ) |
No errors
I assume that there's already a Script
in ServerScriptService
that's creates the leaderstats Folder
.
You don't need to create a new folder just to get the leaderstats, you can get it using Player:WaitForChild("leaderstats")
.
01 | local Players = game:GetService( "Players" ) |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 |
04 | local Player: Player = Players.LocalPlayer |
05 | local ChangedTeam: RemoteEvent = ReplicatedStorage:WaitForChild( "ChangedTeam" ) |
06 |
07 | local button = script.Parent |
08 |
09 | button.MouseButton 1 Click:Connect( function () |
10 | local leaderstats: Folder = Player:WaitForChild( "leaderstats" ) |
11 | local money: IntValue = leaderstats:WaitForChild( "Money" ) |
12 |
13 | if money.Value > = 100 then |
14 | money.Value - = 100 |
15 |
16 | local team: string = button.Name |
17 | ChangedTeam:FireServer(team) |
18 | end |
19 | end ) |
While your actual issue is due to what the other answer already mentioned – I can tell you that your LocalScript
will not deduct any actual money from the user.
This is because of client-server replication. What changes on a client is not replicated to the server, for security purposes, and a LocalScript
– as the name says, only executes Locally or in other words, Client-side.
Nonetheless, to solve this issue of client-server replication you can simply try using a Script instead or resort to making a Remote (with proper sanity-checks to avoid exploitation, of course).