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)
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)
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
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
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)
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)