So i have been scripting a shop and when someone buys something the points go down for a sec and then they go back as the points the user had. This is my script
From starter gui:
1 | local player = script.Parent.Parent |
2 | local leaderboard = player:WaitForChild( "leaderstats" ) |
3 | local points = leaderboard:WaitForChild( "Points" ) |
4 |
5 | while wait( 3 ) do |
6 | points.Value = points.Value+ 1 |
7 | end |
From Workspace:
1 | game.Players.ChildAdded:connect( function (player) |
2 | local stats = Instance.new( "Model" ,player) |
3 | stats.Name = "leaderstats" |
4 | local money = Instance.new( "IntValue" ,stats) |
5 | money.Name = "Points" |
6 | money.Value = 0 |
7 | end ) |
Also there is the script from an item from the shop
01 | script.Parent.MouseButton 1 Click:connect( function () |
02 | local RS = game:GetService( "ReplicatedStorage" ) |
03 | local item = RS:WaitForChild( "Gravity Coil" ) |
04 | local price = 50 |
05 | local player = game.Players.LocalPlayer |
06 | local stats = player:WaitForChild( "leaderstats" ) |
07 |
08 | if stats.Points.Value > = price then |
09 | stats.Points.Value = stats.Points.Value - price |
10 | local cloned = item:Clone() |
11 | local cloned 2 = item:Clone() |
12 | cloned 2. Parent = player.Backpack |
13 | cloned.Parent = player.StarterGear |
14 | end |
15 | end ) |
You need to use a remote event. This is because localscript cannot change an IntValue serverwide, even if the value is located in the player. Add these 2 scripts, and take out ur local script. I am assuming the "Shop" is a Gui Shop. In a script:
01 | local RS = game:GetService( "ReplicatedStorage" ) |
02 | local function createEvent(eventName) |
03 | local event = game.ReplicatedStorage:FindFirstChild(eventName) |
04 | if not event then |
05 | event = Instance.new( "RemoteEvent" , game.ReplicatedStorage) |
06 | event.Name = eventName |
07 | end |
08 | return event |
09 | end |
10 |
11 | local buy = createEvent( "BuyGCoil" ) |
12 |
13 | buy.OnServerEvent:Connect( function (player, price) |
14 | local points = player:FindFirstChild( "leaderstats" ):FindFirstChild( "Points" ) |
15 | local item = RS:WaitForChild( "Gravity Coil" ) |
In a localscript in Gui Shop, in a Ui Button:
1 | local price = 50 |
2 | function onClick() |
3 | local player = game.Players.LocalPlayer |
4 | local stats = player:WaitForChild( "leaderstats" ) |
5 | if stats.Points.Value > = price then |
6 | game.ReplicatedStorage:FindFirstChild( "BuyGCoil" ):FireServer(price) |
7 | end |
8 | end |
9 | script.Parent.Activated:Connect(onClick) |
This generally works great, as long as everything is in the right place. If this isn't what you intended, comment on this to let me know. If this works, then mark this as the answer so others don't waste time! Thank you!