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

Why can't I detect a change in a NumberValue if I change it in a localscript?

Asked by 5 years ago

Hey Community!

I was working on a Roblox game, and I wanted to change a player's leaderstats from a local script. Here I have the script from the GUI text button:

local Button = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character
local price = 50
function onClick()
    if player.trails.Rainbow.Value==0 then
        if player.leaderstats.Cash.Value>=price then
            local stat = player:FindFirstChild("leaderstats")
                if stat then
                    print("yep")
                    local cash = player.leaderstats.Cash.Value - 50
                    player.leaderstats.Cash.Value = cash
                    player.trails.Rainbow.Value = 1
                end
        else
            print("nah")
            script.Parent.Parent.Parent.Visible=false
            script.Parent.Parent.Parent.Parent.Parent.Error.Frame.Visible=true
        end
    end
end

Button.MouseButton1Click:connect(onClick)

When the leaderstats changes here, it shows up in the leaderboard, but the following script does not save it:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")
local ds2 = DataStore:GetDataStore("GemSaveSystem")
local ds3 = DataStore:GetDataStore("RainbowTrailSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local trails = Instance.new("Folder",player)
 trails.Name = "trails"
 local Cash = Instance.new("IntValue",leader)
 Cash.Name = "Cash"
 local Gems = Instance.new("IntValue",leader)
 Gems.Name = "Gems"
 local Rainbow = Instance.new("IntValue",trails)
 Rainbow.Name = "Rainbow"
 Cash.Value = ds:GetAsync(player.UserId) or 0
 Gems.Value = ds2:GetAsync(player.UserId) or 0
 Rainbow.Value = ds3:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, Cash.Value)
 Cash.Changed:connect(function()
  ds:SetAsync(player.UserId, Cash.Value)
 end)
 Gems.Changed:connect(function()
  ds2:SetAsync(player.UserId, Gems.Value)
 end)
 Gems.Changed:connect(function()
  ds3:SetAsync(player.UserId, Rainbow.Value)
 end)
end)

When I edit the leaderstats from a normal script, it detects Cash.Changed, but when I edit it from the localscript, it doesn't. I'm wondering why this is happening, and if anyone knows how to fix this.

Thanks, Armacom03

1
Because Filtering Enabled User#19524 175 — 5y
0
You're changing the cash on the client, so you'll need remotes to get the change server sided. Additionally, don't save the cash each time it changes, if it changes quickly many times you'll throttle the request limit, and use UpdateAsync, not SetAsync User#19524 175 — 5y
0
also don't use the parent parameter of Instance.new theking48989987 2147 — 5y
0
@incapz wdym? Could you send an answer cause I dont understand. armacom03 15 — 5y
0
@incapz my filtering enabled is true. Should I turn it off? armacom03 15 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Assisted through Discord.

Ad

Answer this question