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

Characters not loading upon joining?

Asked by 1 year ago

Hi, basically in my game I have been encountering this problem where if I join or any other player joins it seems, if there were people already playing the game, their characters dont load. What i mean is that they are invisible, they dont make sounds, you cant see the tool they have equipped or anything. The only way to fix it is for the invisible person to reset their character. Obviously, this is very bad. I have noticed, however, that this only occurs if the people already playing are NEW players. I don't really know what the cause of it is but this is the script that loads when the player joins the game, in serverscriptservice. Help would be much appreciated!

local dataStores = game:GetService("DataStoreService"):GetDataStore("PointsDataStore")
local defaultCash = 50
local defaultKills = 0
local defaultWins = 0
local playersLeft = 0

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

    playersLeft = playersLeft  + 1


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

    local ss = Instance.new("BoolValue")
    ss.Name = "Screenshake"
    ss.Parent = player
    ss.Value = true

    local KillStreak = Instance.new("IntValue")
    KillStreak.Name = "Streak"
    KillStreak.Parent = player

    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Value = 0
    Points.Parent = leaderstats

    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Value = 0
    Kills.Parent = leaderstats

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

    local playerData = Instance.new("Folder")
    playerData.Name = player.Name
    playerData.Parent = game.ServerStorage.PlayerData

    local equipped = Instance.new("StringValue")
    equipped.Name = "Equipped"
    equipped.Parent = playerData

    local inventory = Instance.new("Folder")
    inventory.Name = "Inventory"
    inventory.Parent = playerData

    player.CharacterAdded:Connect(function(character)
        character.Humanoid.WalkSpeed = 16
        character.Humanoid.Died:Connect(function()

            -- Whenever somebody dies, this event will run

            KillStreak.Value = 0


            if character.Humanoid and character.Humanoid:FindFirstChild("creator") then



                if player.Character.Humanoid:FindFirstChild("creator") then
                    local tag = player.Character.Humanoid:FindFirstChild("creator")
                    local player2 = tag.Value
                    local kills2 = player2.leaderstats.Kills
                    local streak = player2:FindFirstChild("Streak")
                    local point = player2.leaderstats.Points
                    streak.Value = streak.Value + 1
                    kills2.Value = kills2.Value + 1
                    point.Value = point.Value + 50

                    if player.UserId == 264630904 then
                        game:GetService("BadgeService"):AwardBadge(player2.UserId, 2130186578)
                    end

                    local useri = player2.UserId
                    game.ReplicatedStorage.youkill:FireClient(player2, player.DisplayName)
                    game.ReplicatedStorage.Ow:FireClient(player,player2)
                    game.ReplicatedStorage.ps:FireClient(player2,streak.Value)
                    game.ReplicatedStorage.STREAK:FireAllClients(player2.DisplayName,streak.Value)
                    game.ReplicatedStorage.Feed:FireAllClients(player.DisplayName,player2.DisplayName)
                    if streak.Value == 5 then
                        point.Value = point.Value + 100
                    elseif streak.Value == 10 then
                        point.Value = point.Value + 200
                    elseif streak.Value == 15 then
                        point.Value = point.Value + 300
                    elseif streak.Value == 20 then
                        point.Value = point.Value + 400
                    elseif streak.Value == 30 then
                        point.Value = point.Value + 600
                    elseif streak.Value == 40 then
                        point.Value = point.Value + 800
                    elseif streak.Value == 50 then
                        point.Value = point.Value + 1000
                    elseif streak.Value == 60 then
                        point.Value = point.Value + 1200
                    elseif streak.Value == 70 then
                        point.Value = point.Value + 1400
                    elseif streak.Value == 80 then
                        point.Value = point.Value + 1600
                    elseif streak.Value == 90 then
                        point.Value = point.Value + 1800
                    elseif streak.Value == 100 then
                        point.Value = point.Value + 2000
                    end

                    wait(6)
                    game.ReplicatedStorage.Buttonsvisible:FireClient(player)
                else

                    game.ReplicatedStorage.Buttonsvisible:FireClient(player)
                end

            else
                if character:FindFirstChild("GameTag") then
                    game.ReplicatedStorage.riiiiip:FireAllClients(player.Name)
                    end
            end
            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()
            end


        end)



    end)


    game.ReplicatedStorage.playtimee.OnServerEvent:Connect(function(playername)
        if playername.Name == player.Name then
            playername.leaderstats.Points.Value = playername.leaderstats.Points.Value + 100
            end
    end)


    -- Data stores

    local player_data
    local kills_data
    local wins_data
    local weaponsData
    local equippedData

    pcall(function()
        player_data = dataStores:GetAsync(player.UserId.."-Points")
    end)

    pcall(function()
        kills_data = dataStores:GetAsync(player.UserId.."-Kills")
    end)

    pcall(function()
        wins_data = dataStores:GetAsync(player.UserId.."-Wins")
    end)

    pcall(function()
        weaponsData = dataStores:GetAsync(player.UserId.."-Weps")
    end)

    pcall(function()
        equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue")
    end)

    if player_data ~= nil then
        -- Player has saved data, load it in
        Points.Value = player_data
    else
        -- New player
        Points.Value = defaultCash
    end

    if kills_data ~= nil then
        Kills.Value = kills_data
    else
        Kills.Value = defaultKills
    end

    if wins_data ~= nil then
        Wins.Value = wins_data
    else
        Wins.Value = defaultWins
    end

    if weaponsData then

        -- For loop through the weapons saved
        for _, weapon in pairs(weaponsData) do
            if game.ServerStorage.Items:FindFirstChild(weapon) then
                local weaponClone = game.ServerStorage.Items[weapon]:Clone()
                weaponClone.Parent = inventory
                print(weapon.." Loaded in!")
            end
        end

        if equippedData then
            equipped.Value = equippedData
            player:WaitForChild("PlayerGui")
            game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
        end

    else
        print("No weapons data")

    end

end)

local bindableEvent = Instance.new("BindableEvent")

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

    pcall(function()

        dataStores:SetAsync(player.UserId.."-Points", player.leaderstats.Points.Value)
        print("Saved")

    end)

    pcall(function()

        dataStores:SetAsync(player.UserId.."-Kills", player.leaderstats.Kills.Value)
        print("Saved")

    end)

    pcall(function()

        dataStores:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value)
        print("Saved")

    end)    

    pcall(function()
        local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
        local weaponsTable = {}

        for _, v in pairs(weapons) do
            table.insert(weaponsTable,v.Name)
        end

        dataStores:SetAsync(player.UserId.."-Weps",weaponsTable)

        if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
            local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
            dataStores:SetAsync(player.UserId.."-EquippedValue",equippedVal.Value)

        end
    end)

    print("saved weapons and points!")

    playersLeft = playersLeft - 1
    bindableEvent:Fire()

end)

game:BindToClose(function()
    -- This will be triggered upon shutdown
    while playersLeft > 0 do
        bindableEvent.Event:Wait()
    end
end)
0
Tip: Nobody's going to look at a question with more than 100 lines, especially if you do not tell us which line is likely having the issue. You're going to have to narrow it down for us Shawnyg 4330 — 1y
0
Oh ok. The problem is, I dont really know which line might be the problem though. Ill look more into it but thanks for telling me. DriBowser 55 — 1y

Answer this question