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

If statement trouble, what the heck am I doing wrong??

Asked by
Gojinhan 353 Moderation Voter
5 years ago

Hi everyone, I'm really really confused. For context, I have a gui in starterGui that has a local script that makes it so when a remote (from my datastore script, ill show it later it's huge lmao) is called to the player, a gui comes up for them to choose 1 of 6 colors. When they choose a color, the color button does invokeserver() to the datastore script, the color is sent from the function on the local script to the receiver on the datastore script so thats how it's obtained.

Then the remote function on the datastore sets a saved bool and string values, with the bool being: "HasChosenColor" and the string just being a tostring() color sent from the local script.

I'm probably not explaining this well, english isn't my first language. I think it'd be better if I showed ya the scripts and did a light explanation on each.

Here's the LocalScript which is a child of the Color Choosing ScreenGui:

local player = game.Players.LocalPlayer
local char = player.Character
script.Parent.Frame.Visible = false
script.Parent.Frame.Position = UDim2.new(0.31, 0,1.067, 0)

game.ReplicatedStorage.Remotes.ColorSelectPrompt.OnClientEvent:connect(function(player)
    wait(3)
    char.Humanoid.WalkSpeed = 0
    local fgui1 = script.Parent.Frame
    local fgui1p = script.Parent

    fgui1.Visible = true
    fgui1:TweenPosition(UDim2.new(0.31, 0,0.707, 0))
end)

This script just shows the gui when a remote is fired on the DataStore script. The remote is fired when "HasChosenColor" value inside the player is false.

Before I show the DataStore script here's the LocalScript that's a child of every TextButton. (This local script is the exact same for every button but the Color at the top matches the button.)

local result = tostring("Black")
local plr = game.Players.LocalPlayer
local pchar = plr.Character
script.Parent.Size = UDim2.new(0, 76,0, 74)
script.Parent.MouseButton1Down:connect(function()
    pchar.Humanoid.WalkSpeed = 16
    script.Parent:TweenSize(UDim2.new(0, 90,0, 88))
    script.Parent.Parent.ColorDeepOrange.Active = false
    script.Parent.Parent.ColorPersimmons.Active = false
    script.Parent.Parent.ColorInstituionalWhite.Active = false
    script.Parent.Parent.ColorReallyRed.Active = false
    script.Parent.Parent.ColorElectricBlue.Active = false
    game.ReplicatedStorage.Remotes.ColorSelectResult1:InvokeServer(result)
    script.Parent.Parent.Parent.Frame:TweenPosition(UDim2.new(0.31, 0,1.067, 0))
    script.Parent.Parent.Parent.Frame.Visible = false
    script.Parent.Parent.Parent.Frame.Active = false
end)

That local script just tells the DataStore script that a color was chosen through a remote function.

Here's the biggy, and if you were confused til now you'll probably understand it. There are a lot of things in this script that don't relate to the problem but I thought I should include the full thing because it contains the error. (Warning, huge script)

local maxcount = 500000000000

local dss = game:GetService("DataStoreService"):GetDataStore(REDACTED)
local di = game:GetService("ServerStorage"):WaitForChild("DataIndex")
local AutosaveInterval = 60
game.Players.PlayerAdded:connect(function(player)
    local playerdata = Instance.new("Folder", di)
    playerdata.Name = player.Name

    local din = player.Name

    local stats = Instance.new("Folder", playerdata)
    stats.Name = "Stats1"

    local ArcaneCount = Instance.new("IntValue", stats)
    ArcaneCount.Name = "Arcane"

    local leaderstatfolder = Instance.new("Folder", player)
    leaderstatfolder.Name = "leaderstats"

    local WillpowerCount = Instance.new("IntValue", stats)
    WillpowerCount.Name = "Willpower"

    local HasChosenColor = Instance.new("BoolValue", stats)
    HasChosenColor.Name = "HasChosenColor"

    local Arcin = Instance.new("IntValue", leaderstatfolder)
    Arcin.Name = "Arcine"

    local lac3 = Instance.new("IntValue", leaderstatfolder)
    lac3.Name = "Coins"

    local lac2 = Instance.new("IntValue", leaderstatfolder)
    lac2.Name = "Willpower"


    local CoinsCount = Instance.new("IntValue", stats)
    CoinsCount.Name = "Coins"

    local ChosenColorB = Instance.new("StringValue", leaderstatfolder)
    ChosenColorB.Name = "ChosenColorB"

    local OtherData = Instance.new("Folder", playerdata)
    OtherData.Name = "Other"

    --/Loading data
    local SavedData = dss:GetAsync(player.userId)
    --local datatable = {SavedData}

    local BrickCooler = Instance.new("BrickColorValue", OtherData)
    BrickCooler.Name = "BrickCooler"

    if HasChosenColor.Value == false then
        game.ReplicatedStorage.Remotes.ColorSelectPrompt:FireClient(player)
    else
        print("cmon put it")
        BrickCooler.Value = BrickColor.new(ChosenColorB.Value)
    end

    --if HasChosenColor.Value == false then
        --game.ReplicatedStorage.Remotes.ColorSelectPrompt:FireClient(player)

    --else
        --di[pname1].OtherData.PlayerColorB.Value = BrickColor.new()
    --end

    game.ReplicatedStorage.Remotes.MaxArcane1.OnServerInvoke = function(player, pname)
        di[pname].Stats1.Arcane.Value = game.ServerStorage.DataIndex[pname].Stats1.Arcane.Value + maxcount
    game.Players[pname].leaderstats.Arcine.Value = game.Players[pname].leaderstats.Arcine.Value + maxcount
    end



    game.ReplicatedStorage.Remotes.MaxWillpower1.OnServerInvoke = function(player, pname)   
    di[pname].Stats1.Willpower.Value = game.ServerStorage.DataIndex[pname].Stats1.Willpower.Value + maxcount
    game.Players[pname].leaderstats.Willpower.Value = game.Players[pname].leaderstats.Willpower.Value + maxcount
    end

    game.ReplicatedStorage.Remotes.GlobalResetStats.OnServerEvent:connect(function()
        ArcaneCount.Value = 0
        WillpowerCount.Value = 0
        Arcin.Value = 0
        lac2.Value = 0
        lac3.Value = 0
        CoinsCount.Value = 0
    end)

    game.ReplicatedStorage.Remotes.ResetStats.OnServerInvoke = function(player, pname)
        di[pname].Stats1.Willpower.Value = 0
        game.Players[pname].leaderstats.Willpower.Value = 0
        di[pname].Stats1.Coins.Value = 0
        game.Players[pname].leaderstats.Coins.Value = 0
        di[pname].Stats1.Arcane.Value = 0
        game.Players[pname].leaderstats.Arcine.Value = 0
    end
    game.ReplicatedStorage.Remotes.IncreaseArcane1.OnServerInvoke = function(player, pname)
        di[pname].Stats1.Arcane.Value = di[pname].Stats1.Arcane.Value + 1
        game.Players[pname].leaderstats.Arcine.Value = player.leaderstats.Arcine.Value + 1
    end

    game.ReplicatedStorage.Remotes.IncreaseWillpower1.OnServerInvoke = function(player, pname)
        di[pname].Stats1.Willpower.Value = di[pname].Stats1.Willpower.Value + 5
        game.Players[pname].leaderstats.Willpower.Value = player.leaderstats.Willpower.Value + 5
    end


    if SavedData then
        print('Data currently used:'..game:GetService("HttpService"):JSONEncode(SavedData):len()..'/260000 bytes')
        for i,v in pairs(SavedData) do
            print(i,v)
            if i % 2 == 0 then
                di[player.Name].Stats1:FindFirstChild(SavedData[i - 1]).Value = v
            end
        end
        Arcin.Value = ArcaneCount.Value
        lac2.Value = WillpowerCount.Value
        lac3.Value = CoinsCount.Value
    end
end)



local function SaveStats(player)
    local DataCollective = {}
    for i,v in pairs(di[player.Name].Stats1:GetChildren()) do
        DataCollective[#DataCollective + 1] = v.Name
        DataCollective[#DataCollective + 1] = v.Value    
    end 
    --dss:SetAsync(player.userId, game:GetService("HttpService"):JSONEncode(DataCollective))
    local pnameout = player.Name
    print(pnameout.."'s Data was saved.")
    dss:SetAsync(player.userId, DataCollective)
end

game.ReplicatedStorage.Remotes.ColorSelectResult1.OnServerInvoke = function(player, result)

    local din = player.Name
        di[din].Stats1.HasChosenColor.Value = true
        game.Players[din].leaderstats.ChosenColorB.Value = result
        di[din].Other.BrickCooler.Value = BrickColor.new(result)
end

--game.ReplicatedStorage.Remotes.SaveStats1.OnServerEvent:connect(function()
    --for i, player in pairs(game.Players:GetPlayers()) do
        --SaveStats(player)
    --end
--end)


game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        SaveStats(player)
    end
end)
game.Players.PlayerRemoving:connect(function(player)
    SaveStats(player) --/Saving data upon the player leaving.
end)

while wait(AutosaveInterval) do
    for i, player in pairs(game.Players:GetPlayers()) do
        SaveStats(player)
    end
end



Welp, that was big. Anyway, things important to note is a value ChosenColorB is created which acts as the color that is sent from the local script. First thing you'll notice is that BrickCooler (the brickcolor value) isnt saved with the rest. That's intentional because i can't save BrickColor values. So when the remote function from the local script is invoked the brickcolor value is set to the ChosenColorB value, and here's the main problem. "HasChosenColor" is set to true when that remote is invoked.

And I have an if statement higher up in the script at around Line 53. When the game is done doing all the loading the if statement checks if HasChosenColor is false. Naturally it shouldn't be, everytime I choose a color, save, then exit and reload the game the output reads:

Data currently used:58/260000 bytes 1 Arcane 2 0 3 Willpower 4 0 5 HasChosenColor 6 true 7 Coins 8 0

And that's where the madness begins, if it's true and the game knows it, why is the If statement not recognizing that? It's breaking everything and it's so annoying ;-; pls help

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

We talked it over on discord for anyone wondering. The issue was that we weren't loading the data from the datastore

we saved the data in a table like this:

DataCollective = {
    ["Arcane"] = 10,
    ["ChosenColorA"] = 10,
    ["Coins"] = 10,
    ["HasChosenColor"] = 10,
    ["Willpower"] = 10        
}

and after we got the data with getasync() we assigned the data in the datastore to the object values in the game using this function:

function assignDataToFolder(player, data)
    for _, variable in pairs(di[player.Name].Stats1:GetChildren())do
        variable.Value = data[variable.Name]
    end
end

if anyone else reading this post has a similar issue, let me know on discord, Lucy# 4 8 5 4

Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
5 years ago

The problem is that you are reading the HasChosenColor value, BEFORE it is read from your saving storage. To make it work, you will need to move whole statement:

    if HasChosenColor.Value == false then
        game.ReplicatedStorage.Remotes.ColorSelectPrompt:FireClient(player)
    else
        print("cmon put it")
        BrickCooler.Value = BrickColor.new(ChosenColorB.Value)
    end

AFTER the read data segment (below line 118).

Answer this question