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 4 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:

local player=script.Parent.Parent
local leaderboard=player:WaitForChild("leaderstats")
local points=leaderboard:WaitForChild("Points")

while wait(3)do
 points.Value=points.Value+1
end

From Workspace:

game.Players.ChildAdded:connect(function(player)
 local stats = Instance.new("Model",player)
 stats.Name="leaderstats"
 local money = Instance.new("IntValue",stats)
 money.Name="Points"
 money.Value=0 
end)

Also there is the script from an item from the shop

script.Parent.MouseButton1Click:connect(function()
 local RS = game:GetService("ReplicatedStorage")
 local item =  RS:WaitForChild("Gravity Coil")
 local price = 50
 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")

if stats.Points.Value >= price then 
  stats.Points.Value = stats.Points.Value - price
  local cloned = item:Clone()
  local cloned2 = item:Clone()
  cloned2.Parent = player.Backpack
  cloned.Parent = player.StarterGear
 end
end)
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 — 4y
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 — 4y

1 answer

Log in to vote
2
Answered by 4 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:

    local RS = game:GetService("ReplicatedStorage")
local function createEvent(eventName)
        local event = game.ReplicatedStorage:FindFirstChild(eventName)
    if not event then
            event = Instance.new("RemoteEvent", game.ReplicatedStorage)
            event.Name = eventName
        end
        return event
    end

    local buy= createEvent("BuyGCoil")

    buy.OnServerEvent:Connect(function(player, price)
    local points = player:FindFirstChild("leaderstats"):FindFirstChild("Points")
     local item =  RS:WaitForChild("Gravity Coil")
        points.Value = points.Value - price
    local clone = item:Clone()
    local SPclone = item:Clone()
    clone.Parent = player.Backpack
    SPclone.Parent = player.StarterGear
    end


In a localscript in Gui Shop, in a Ui Button:

    local price = 50
    function onClick()
    local player = game.Players.LocalPlayer
     local stats = player:WaitForChild("leaderstats")
    if stats.Points.Value >= price then
    game.ReplicatedStorage:FindFirstChild("BuyGCoil"):FireServer(price)
    end
    end
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!

0
Please clean this up Ziffixture 6913 — 4y
0
Does the first script has to be in an ui button? Shadic1270 136 — 4y
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 — 4y
0
Listen to SethHienzman. The first script can go anywhere, really, but ServerScriptService is the best place to put it. Heavenlyblobmaster 271 — 4y
0
Got to do some fixes, but it actually worked, thank you! Shadic1270 136 — 4y
Ad

Answer this question