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

If IntValue.Value == 10 then -- Not working as expected?

Asked by 4 years ago

Hello, i have a problem that i can't seem to figure out, and this has been a problem for quite a while now, but i decided to ask for help this time!

Anyways, here's the whole script and where everything is located.


This Script Is Located In game.ServerScriptService.

-- DataStoring Two IntValues

--Everything works fine with this code, nothing wrong with it, just going to put it here incase there has to be anything changed.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStoreNew")

game.Players.PlayerAdded:Connect(function(player)

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

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    local XP = Instance.new("IntValue")
    XP.Name = "XP"
    XP.Parent = leaderstats

    -- Load Data

    local playerUserId = "Player_"..player.UserId

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)   
    end)

    if success then
        Cash.Value = data 
        XP.Value = data
        print("Player data successfully loaded!")
    else
        print("There was an error whilst loading player data!")
        warn(errormessage)
    end

end)


game.Players.PlayerRemoving:Connect(function(player)

    local playerUserId = "Player_"..player.UserId

    local cashData = player.leaderstats.Cash.Value
    local xpData = player.leaderstats.XP.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, cashData, xpData)
    end)
    if success then
        print("Player data successfully saved!")
    else
        print("There was an error whilst saving player data!")
        warn(errormessage)
    end
end)

This Script Is Located In : game.StarterPlayer.StarterPlayerScripts.

local player = game.Players.LocalPlayer


wait(3) -- Making sure the player has loaded otherwise an error will pop up
if player.leaderstats.XP.Value == 10 then
    print("Player has successfully reached 10 XP!")
end

Thanks for reading through! Hope you can resolve this issue that i'm having.

1 answer

Log in to vote
0
Answered by 4 years ago

Yes, i found the problem! It was quite silly!

local player = game.Players.LocalPlayer


wait(3) -- Making sure the player has loaded otherwise an error will pop up
while wait() do
    if player.leaderstats.XP.Value == 10 then
        print("Player has successfully reached 10 XP!")
        break
    end 
end

FIX : Adding a while loop to make sure its endlessly checking if the XP.Value == 10.

0
You can also do repeat wait() until player and player.Character to wait for the player to load then use changed event to check instead of loop... Feelings_La 399 — 4y
0
or do: while player.leaderstats.XP.Value <= 10 and wait() do print("Player has successfully reached 10 XP!") break end papasherms 168 — 4y
Ad

Answer this question