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

Why doesn't my game's DataStore work? Output says nothing?

Asked by 9 years ago

Hey. So for my game, it is supposed to use datastores and award BlitzCreditz to the player for winning a round. Everything works fine up until line 208, when it is supposed to give the credits to the player. It doesn't award anything or update the leaderboard. I have tried everything at this point! Please no annoying vague answers, not to be picky but I ask that you please directly fix the problems, I'm a very novice scripter. Here's my code; sorry it's long.

local roundtimer = 90
local intermissiontimer = 5

local datastore = game:GetService("DataStoreService"):GetDataStore("playerdata")
local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local maps = serverstorage:WaitForChild("Maps")
local mapholder = game.Workspace:WaitForChild("MapHolder")
local gear = serverstorage:WaitForChild("Gear")
local statustag = replicatedstorage:WaitForChild("Status")
local timertag = replicatedstorage:WaitForChild("TimerTag")
local Winner = replicatedstorage:WaitForChild("Winner")
local resulttag = replicatedstorage:WaitForChild("result")
local event = replicatedstorage:WaitForChild("RemoteEvent")
local firstrun = true



local playerdata = {}



function awardwin(players)
    if type(players) == type({}) then

        for i = 1, #players do
    if players[i]:FindFirstChild("MatchTag") then
            if players[i]:FindFirstChild('leaderstats') then
                if  players[i].leaderstats:FindFirstChild('BlitzCreditz') then 
                    local stat = players[i].leaderstats.Wins
                    stat.Value = stat.Value + 1
                    datastore:UpdateAsync(players.userId).Wins = datastore:UpdateAsync(players.userId).Wins + 1
                end
            end
        end
    end
end
end

function awardcredit(players, credits)
    if type(players) == type({}) then

        for i = 1, #players do
            if players[i]:FindFirstChild("MatchTag") then
            if players[i]:FindFirstChild('leaderstats') then
                if  players[i].leaderstats:FindFirstChild('BlitzCreditz') then 
                    local stat = players[i].leaderstats.BlitzCreditz 
                    stat.Value = stat.Value + credits 
                    datastore:UpdateAsync(players.userId).BC = datastore:UpdateAsync(players.userId).BC + credits
                end
            end
        end
    end
end
end





function playeradded(player)

    local mydata = datastore:GetAsync(player.userId) or {}
    mydata.wins = mydata.wins or 0
    mydata.BC = mydata.BC or 0
    mydata.hyperlaser = mydata.hyperlaser or 0

    playerdata[player.userId] = mydata


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


    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = mydata.wins
    wins.Parent = leaderstats

    local hyperlaser = Instance.new("IntValue")
    hyperlaser.Name = "Hyperlaser"
    hyperlaser.Value = mydata.hyperlaser
    hyperlaser.Parent = player

    local BC = Instance.new("IntValue")
    BC.Name = "BlitzCreditz"
    BC.Value = mydata.BC
    BC.Parent = leaderstats




end
game.Players.PlayerAdded:connect(playeradded)
for _, player in pairs(game.Players:GetPlayers()) do
    playeradded(player)
end 

if firstrun == true then
    wait(15)
    firstrun = false
end

function permsave(player)
    if player then
        local mydata = playerdata[player.userId]
        if mydata then
            datastore:SetAsync(player.userId, mydata)
        end
    end
end

game.Players.PlayerRemoving:connect(function(player)
    if player then
        permsave(player)
        playerdata[player.userId] = nil
    end
end)

game.OnClose = function()
    for i, v in pairs(playerdata) do
        datastore:SetAsync(i, v)
    end
end

while true do 
  --Waits 1 minute in between games
 m = Instance.new("Message") --Creates a new message
  local players = game.Players:GetChildren() 
wait(intermissiontimer)
  m.Parent = game.Workspace 
  m.Text = "Choosing Map..." 
  wait(3)
  m.Text = "Map is 'The Classic Map'"
wait(3)
  m.Parent = nil
 mapholder:ClearAllChildren()
wait(1)
local allmaps = maps:GetChildren()
local newmap = allmaps[math.random(1, #allmaps)]:clone()
newmap.Parent = mapholder
print("FoundRandomMap")

contestants = {}
while true do
    print("Finding Players")
        for _,  player in pairs(game.Players:GetPlayers()) do
        if player and player.Character then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            if humanoid and humanoid.Health > 0 then
                table.insert(contestants, player)
            end
        end


        end

            if #contestants >= 1 then
            break
            end
            wait(1)
            end

  for _, player in pairs (contestants)do
if player and player.Character then
local torso = player.Character:FindFirstChild("Torso")
local humanoid = player.Character:FindFirstChild("Humanoid")
if torso and humanoid then
humanoid.Health = 100
end
local matchtag = Instance.new("StringValue")
matchtag.Name = "MatchTag"
matchtag.Parent = player.Character
local backpack = player:FindFirstChild("Backpack")
if backpack then
local allgear = gear:GetChildren()
local randomgear = allgear[math.random(1, #allgear)]:clone()
randomgear.Parent = backpack

torso.CFrame = CFrame.new(newmap:WaitForChild("floor").Position + Vector3.new(0, 10, 0))
print("Teleported player")
end
end

local localtimer = roundtimer
while localtimer > 0  do
    print("Run")
    wait(1)
    localtimer = localtimer - 1
    activecontestants = {}
    for _, player in pairs(contestants) do
        if player then
            local character = player.Character
            if character then
                local humanoid = character:FindFirstChild("Humanoid")
                local matchtag = character:FindFirstChild("MatchTag")
            if matchtag and humanoid and humanoid.Health > 0 then
                table.insert(activecontestants, player)
            end
            end
        end
    end
    if #activecontestants <= 1 then
        awardcredit(players, 4)
        awardwin(players)
        break

    end
end
wait(5)
for _, player in pairs(contestants) do
    if player and player.Character then
        local humanoid = player.Character:FindFirstChild("Humanoid")
        local matchtag = player:FindFirstChild("MatchTag")
        if matchtag then 
            player.Character:MoveTo(Vector3.new(-73.024, 4.478, 144.138))
        end
        if humanoid then
            humanoid:UnequipTools()

        end
    end
    local backpack = player:FindFirstChild("Backpack")
    if backpack then
        backpack:ClearAllChildren()
        print("Cleared backpack")
    end

    end


mapholder:ClearAllChildren()

  m.Parent = game.Workspace
  m.Text = "Blitz over! "
  wait(3)
  m.Text = "You have 10 seconds until the start of the next Blitz."
  wait(3)
  m.Parent = nil
    m:remove()
wait(2)
end

end

Answer this question