What do you want to achieve? Keep it simple and clear!
I want to make a script that changes the value of a NumberValue when the NumberValue is bigger than the MaxAmount.
What is the issue? Include screenshots / videos if possible!
The issue is that it doesn’t work.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I couldn’t find any solutions.
-- Services
local Players = game:GetService("Players")
-- Variables
local MaxGold = 100000000000
local MaxDiamonds = 500000
-- Functions
Players.PlayerAdded:Connect(function(Player)
-- Values - Folders
local DataFolder = Player:FindFirstChild("DataFolder")
local Gold = DataFolder.Gold
local Diamonds = DataFolder.Diamonds
-- :PropertyChangedSignal() - Conditions
Gold:GetPropertyChangedSignal("Value"):Connect(function()
if Gold.Value > MaxGold then
Gold.Value = MaxGold
elseif Gold.Value < 0 then
Gold.Value = 0
end
end)
Diamonds:GetPropertyChangedSignal("Value"):Connect(function()
if Diamonds.Value > MaxDiamonds then
Diamonds.Value = MaxDiamonds
elseif Diamonds.Value < 0 then
Diamonds.Value = 0
end
end)
end)
Also, here is the error: lua ServerScriptService.Data.Currency.CurrencySecurity:13: attempt to index nil with 'Gold'
The error seems like gold doesn’t exist, so I would check to see if the values exist in game. If they don’t add do local Gold = DataFolder:WaitForChild("Gold")
-- Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
-- Data Stores
local GoldData = DataStoreService:GetDataStore("GoldCoinsDataStore")
local DiamondsData = DataStoreService:GetDataStore("Diamonds")
-- Functions
Players.PlayerAdded:Connect(function(Player)
-- Instances
local DataFolder = Instance.new("Folder", Player)
DataFolder.Name = "DataFolder"
local Gold = Instance.new("NumberValue", DataFolder)
Gold.Name = "Gold"
local Diamonds = Instance.new("NumberValue", DataFolder)
Diamonds.Name = "Diamonds"
-- Data Table
local Data
pcall(function()
Data = {
GoldData:GetAsync(Player.UserId);
DiamondsData:GetAsync(Player.UserId);
}
end)
end)
Players.PlayerRemoving:Connect(function(Player)
-- Data Table
local Data = {
Player.DataFolder.Gold.Value;
Player.DataFolder.Diamonds.Value;
}
local success, errormessage = pcall(function()
GoldData:SetAsync(Player.UserId, Data[1])
DiamondsData:SetAsync(Player.UserId, Data[2])
end)
if success then
print("Successfully saved data for "..Player.Name.."(#"..Player.UserId..")")
else
warn(errormessage)
end
end)