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

Points from leaderstats not going down after an item bought?

Asked by 5 years ago

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:

1local player=script.Parent.Parent
2local leaderboard=player:WaitForChild("leaderstats")
3local points=leaderboard:WaitForChild("Points")
4 
5while wait(3)do
6 points.Value=points.Value+1
7end

From Workspace:

1game.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
7end)

Also there is the script from an item from the shop

01script.Parent.MouseButton1Click: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 
08if stats.Points.Value >= price then
09  stats.Points.Value = stats.Points.Value - price
10  local cloned = item:Clone()
11  local cloned2 = item:Clone()
12  cloned2.Parent = player.Backpack
13  cloned.Parent = player.StarterGear
14 end
15end)
0
Did you check to see if that if statement is being ran? That’s a good strategy to see what’s wrong. Put a print statement in there and check it out. SethHeinzman 284 — 5y
0
But yeah this is because you are trying to change values in a local script. Just send it to the server and with a FireServer event and make set the values in the server script SethHeinzman 284 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago

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")
02local 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")
View all 21 lines...

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
9script.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!

0
Please clean this up Ziffixture 6913 — 5y
0
Does the first script has to be in an ui button? Shadic1270 136 — 5y
0
The first script goes is a normal script not a local script. You should put it in serverscriptservice. What is happening is local scripts can’t communicate to the server, so you need to send it to a server script so that the server script can communicate with the server. That is done through remote events. SethHeinzman 284 — 5y
0
Listen to SethHienzman. The first script can go anywhere, really, but ServerScriptService is the best place to put it. Heavenlyblobmaster 271 — 5y
0
Got to do some fixes, but it actually worked, thank you! Shadic1270 136 — 5y
Ad

Answer this question