1
votes

So.. I've been coding to make a GUI show the quantity of currency of a player, the datastore API works perfectly but the local script doesn't (it's local because else it would just update it each time a player's currency gets updated and would be a mess being the opposite of what I want to) and well... sometimes it loads the currency into the GUI but other times it just stays on the original text: "Label" instead of my current currency (4600) here's the proof

What normally happens and should always happen

No script output with currency loaded in GUI

What sometimes happens and shouldn't happen:

Script output with original text "label" instead of current currency "4600" here's the script, I've tried putting waits on the start but the original code is inside the while true do..

wait(game.Players.LocalPlayer:WaitForChild("Data")
wait(game.Players.LocalPlayer.Data:WaitForChild("Bells"))
while true do
    script.Parent.TextLabel.Text = game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Value
    wait() --wait is for not making the loop break and stop the whole script
end

well.. if you want to see if data is really in the player, here's the script, it requires a API (DataStore2)

--[Animal Crossing Roblox Edition Data Store]--
            --Bryan99354--
        --Module not mine--
--Made with a AlvinBlox tutorial--
--·.·.*[Get Data Store, do not erase]*.·.·--

local DataStore2 = require(1936396537)

--[Default Values]--

local DefaultValue_Bells = 300
local DefaultValue_CustomClothes = 0

--[Data Store Functions]--

game.Players.PlayerAdded:Connect(function(player)
    --[Data stores]--
    local BellsDataStore = DataStore2("Bells",player)

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

    Bells = Instance.new("IntValue",Data)
    Bells.Name = "Bells"

    local CustomClothesDataStore = DataStore2("CustomClothes",player)

    local CustomClothes = Instance.new("IntValue",Data)
    CustomClothes.Name = "CustomClothes"

    local function CustomClothesUpdate(UpdatedValue)
        CustomClothes.Value = CustomClothesDataStore:Get(UpdatedValue)
    end

    local function BellsUpdate(UpdatedValue)
        Bells.Value = BellsDataStore:Get(UpdatedValue)
    end

    BellsUpdate(DefaultValue_Bells)
    CustomClothesUpdate(DefaultValue_CustomClothes)

    BellsDataStore:OnUpdate(BellsUpdate)
    CustomClothesDataStore:OnUpdate(CustomClothesUpdate)
end)

--[test and reference functions]--

workspace.TestDevPointGiver.ClickDetector.MouseClick:Connect(function(player)
    local BellsDataStore = DataStore2("Bells",player)
    BellsDataStore:Increment(50,DefaultValue_Bells)
end)
workspace.TestDevCustomClothesGiver.ClickDetector.MouseClick:Connect(function(player)
    local CustomClothesDataStore = DataStore2("CustomClothes",player)
    CustomClothesDataStore:Increment(50,DefaultValue_CustomClothes)
end)

the code that creates "Data" and "Bells" is located in the comment: Data Stores the only script that gets the issue is the short one with no reason :< I hope that you can help me :3 @Night94 I tryed your script but it also failed sometimes New script with same random script output error

1

1 Answers

0
votes

The syntax in your LocalScript is a little off with the waits. With that fixed, it works every time. Also, I would use an event handler instead of updating the value with a loop:

game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Changed:Connect(function(value)
    script.Parent.TextLabel.Text = value
end)