Data Isn't Saving When Value Changed

Hello, Does Anyone Know What’s Wrong With The Code?

The Script Doesn’t Output Anything In Console And Data Isn’t Saved
It’s Supposed To Print Something And Save Data When The Fuel Value Is Changed

Here Is Code:

local Settings = require(script.Parent.Fuel_Settings.Settings)
local PlayerDataHandler = require(game.ReplicatedStorage.PlayerDataHandler)

local Fuel = script.Fuel
local Seat = script.Parent

if Settings.Random_Fuel == true then
	Fuel.Value = math.random(25,60)
	wait(.5)
	while true do
		wait(0)
		if Seat.Throttle == 1 and Fuel.Value > 0 then
			wait(math.random(Settings.Min_Mpg, Settings.Max_Mpg))
			Fuel.Value = Fuel.Value - 1 
		end
	end
else if Settings.Random_Fuel == false then
		Fuel.Value = 15
		while true do 
			wait(0)
			if Seat.Throttle == 1 and Fuel.Value > 0 then
				wait(math.random(Settings.Min_Mpg,Settings.Max_Mpg))
				Fuel.Value = Fuel.Value - 1 
			end
		end
	end
end

Fuel.Value.Changed:Connect(function(player)
	print("Changed")
	PlayerDataHandler:Update(player, "Fuel", Fuel.Value)
end)
5 Likes

maybe it’s because of the loop

3 Likes

yeah you might be right, the while loop might not be allowing the script to get to the end

2 Likes

this should work

local Settings = require(script.Parent.Fuel_Settings.Settings)
local PlayerDataHandler = require(game.ReplicatedStorage.PlayerDataHandler)

local Fuel = script.Fuel
local Seat = script.Parent
task.spawn(function()
	if Settings.Random_Fuel == true then
		Fuel.Value = math.random(25,60)
		wait(.5)
		while true do
			wait(0)
			if Seat.Throttle == 1 and Fuel.Value > 0 then
				wait(math.random(Settings.Min_Mpg, Settings.Max_Mpg))
				Fuel.Value = Fuel.Value - 1 
			end
		end
	else if Settings.Random_Fuel == false then
			Fuel.Value = 15
			while true do 
				wait(0)
				if Seat.Throttle == 1 and Fuel.Value > 0 then
					wait(math.random(Settings.Min_Mpg,Settings.Max_Mpg))
					Fuel.Value = Fuel.Value - 1 
				end
			end
		end
	end
end)
Fuel.Value.Changed:Connect(function(player)
	print("Changed")
	PlayerDataHandler:Update(player, "Fuel", Fuel.Value)
end)
3 Likes

OK So I Tried The Code But Data Still Isn’t Saving And Console Doesn’t Output Anything

1 Like

By default, Settings.Random_Fuel is set to true?

2 Likes

try to see if this works??
\\\\\\\\\\\\\\\\

1 Like

Here Is Fuel Settings Module

local Settings = {
	
	
	Random_Fuel = false; 
	
	
	Min_Mpg = 15; -- The minimum MPG a car will use. 
	Max_Mpg = 20; -- The maximum MPG a car will use. 
	
	Does_Refuel_Cost_Money = true;
	
	Min_Price_Gas = 6; -- Mininmum gas price
	Max_Price_gas = 15; -- Maximum gas price
}

return Settings

change this

Fuel.Value.Changed:Connect(function(player)
	print("Changed")
	PlayerDataHandler:Update(player, "Fuel", Fuel.Value)
end)

to this

Fuel.Changed:Connect(function(value)
	print("Changed")
	PlayerDataHandler:Update(player, "Fuel", Fuel.Value)  --get the player in another way
end)
1 Like

Ah! -sees issue- The issue in the code is that the event connection for Fuel.Value.Changed is missing the parameter for the new value, and the else if statement should be replaced with elseif Xx

1 Like

Thank You Guys For Helping Me. I Managed To Fix It. Here Is The Code:

local Settings = require(script.Parent.Fuel_Settings.Settings)
local PlayerDataHandler = require(game.ServerScriptService.PlayerDataHandler)

local Fuel = script.Fuel
local Seat = script.Parent

local Player = nil

local RealFuel = nil

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant then
		Player = game:GetService("Players"):GetPlayerFromCharacter(Seat.Occupant.Parent)
		Seat:SetAttribute("Player", Player.UserId)
	else
		Player = game:GetService("Players"):GetPlayerByUserId(Seat:GetAttribute("Player"))
	end
	RealFuel = PlayerDataHandler:Get(Player, "Fuel")
	Fuel.Value = RealFuel
end)

Seat.ChildAdded:Connect(function()
	Fuel.Changed:Connect(function()
		PlayerDataHandler:Update(Player, "Fuel", function(currentFuel)
			currentFuel = Fuel.Value
			return currentFuel
		end)
	end)
end)

task.spawn(function()
	if Settings.Random_Fuel == true then
		Fuel.Value = math.random(25,60)
		wait(.5)
		while true do
			wait(0)
			if Seat.Throttle == 1 and Fuel.Value > 0 then
				wait(math.random(Settings.Min_Mpg, Settings.Max_Mpg))
				Fuel.Value = Fuel.Value - 1
			end
		end
	else if Settings.Random_Fuel == false then
			while true do 
				wait(0)
				if Seat.Throttle == 1 and Fuel.Value > 0 then
					wait(math.random(Settings.Min_Mpg,Settings.Max_Mpg))
					Fuel.Value = Fuel.Value - 1
				end
			end
		end
	end
end)
1 Like

what did you change may i ask?

1 Like

So Basically I Moved The Fuel.Changed Function Above The Loops, Changed Method Of Getting Player and It Started Printing In Console And Saving Data Too. Here Is Finished Code:

local Settings = require(script.Parent.Fuel_Settings.Settings)
local PlayerDataHandler = require(game.ServerScriptService.PlayerDataHandler)

local Fuel = script.Fuel
local Seat = script.Parent

local Player = nil

local RealFuel = nil

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant then
		Player = game:GetService("Players"):GetPlayerFromCharacter(Seat.Occupant.Parent)
		Seat:SetAttribute("Player", Player.UserId)
	else
		Player = game:GetService("Players"):GetPlayerByUserId(Seat:GetAttribute("Player"))
	end
	RealFuel = PlayerDataHandler:Get(Player, "Fuel")
	Fuel.Value = RealFuel
end)

Seat.ChildAdded:Connect(function()
	Fuel.Changed:Connect(function()
		PlayerDataHandler:Update(Player, "Fuel", function(currentFuel)
			currentFuel = Fuel.Value
			return currentFuel
		end)
	end)
end)

task.spawn(function()
	if Settings.Random_Fuel == true then
		Fuel.Value = math.random(25,60)
		wait(.5)
		while true do
			wait(0)
			if Seat.Throttle == 1 and Fuel.Value > 0 then
				wait(math.random(Settings.Min_Mpg, Settings.Max_Mpg))
				Fuel.Value = Fuel.Value - 1
			end
		end
	else if Settings.Random_Fuel == false then
			while true do 
				wait(0)
				if Seat.Throttle == 1 and Fuel.Value > 0 then
					wait(math.random(Settings.Min_Mpg,Settings.Max_Mpg))
					Fuel.Value = Fuel.Value - 1
				end
			end
		end
	end
end)

yeah that was what I said to do but i dont think you saw :sob: :sob:

1 Like

yeah idk lol i almost smashed my pc cuz the script didn’t work. i was trying to fix it for 2 days xd

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.