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

Player Spawning with weapons that saved?...

Asked by
lucas4114 607 Moderation Voter
8 years ago

Idk what to make the title... So this is my first time trying to make data save in my games. I made this, the "HasPistol", "HasAutoPistol", "HasSubMachineGun", "Points","Kills" are in each player when they join, so the problem is that It doesn't work... :/ When I reset in game then my guns load but still not points, or kills..... And nuthing in output. :c


function GiveWeapons(plr) if plr:WaitForDataReady() then wait(1) plr.leaderstats.Points.Value = plr.Points.Value plr.leaderstats.Kills.Value = plr.Kills.Value plr.CharacterAdded:connect(function(Plr) if plr.HasPistol.Value == true then wait(0.03) local Gun = game.ServerStorage.Pistol:Clone() Gun.Parent = plr.Backpack print(" "..plr.Name.."'s Pistol is loaded") end if plr.HasAutoPistol.Value == true then wait(0.03) local Gun = game.ServerStorage.AutoPistol:Clone() Gun.Parent = plr.Backpack print(" "..plr.Name.."'s Auto Pistol is loaded") end if plr.HasSubMachineGun.Value == true then wait(0.03) local Gun = game.ServerStorage.SubMachineGun:Clone() Gun.Parent = plr.Backpack print(" "..plr.Name.."'s Sub Machine Gun is loaded") end end) end end game.Players.PlayerAdded:connect(GiveWeapons)

1 answer

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

I assume from your post that you're using DataPersistence, which is much more limited than the newer saving system, DataStoreService. The following answer is completely making use of data stores, instead of persistence, and I recommend you learn to use it.

I had already answered a similar question here. Most of the logic and ideology from that post will be ported here... I attempted to revise and shorten this version, however, with a much more insightful description of the process.


Everytime the PlayerAdded event is fired, the getData function defined in the snippet is called with the argument being the newly added player. The returned package is set as value to the local variablearmory.

Iterating through everything in armory, or the table returned from the calling of getData, every value is passed as an argument to the FindFirstChild method of ReplicatedStorage in search for a direct-descendant with the name of the value. The matching tool is cloned and then parented to the player's Backpack.

When the PlayerRemoving, a quick iteration of his backpack is engaged in search for his newly acquired weapons. When found, the names of said weapons are added into the table output, which is then saved to the data stores for further use.


local dataStore = game:GetService('DataStoreService')
local storage = game:GetService('ReplicatedStorage')
local weapons = dataStore:GetDataStore('Weapons')
local players = game:GetService('Players')

local updateData = function (player, package)
    local key = (player.userId)

    if type(package) == type({}) then
        weapons:UpdateAsync(key, function (old)
            return package
        end)
    end
end

local getData = function (player)
    local key = (player.userId)
    return weapons:GetAsync(key) or {}
end 

players.PlayerAdded:connect(function (player)
    local backpack = player.Backpack
    local armory = getData(player)

    for index, value in ipairs(armory) do
        if storage:FindFirstChild(value) then
            local match = value:Clone()
            match.Parent = backpack
        end
    end
end)


players.PlayerRemoving:connect(function (player)
    local backpack = player.Backpack
    local output = {}

    for index, value in pairs(backpack:GetChildren()) do
        if value:isA('Tool') then
            output[#output + 1] = value.Name
        end
    end
    updateData(player, output)
end)

game.OnClose = function() wait(5) end

N.B: code not tested, will answer questions or edit if anything missing.

EDIT I noticed your weapons were stored in ServerStorage instead of ReplicatedStorage, so make sure to edit it to match your specific purposes.

This approach also saves you the hassle of checking if the user has specific weapons, as you're doing in your original post.

0
I, kind of, understand this.... It's my first time making things save so I'll try using data stores then.. Data persistence was easyer though.. :p lucas4114 607 — 8y
0
Yeah, DataPersistence allowed the saving of instances, but with more practice, you'll understand how to use the DataStoreService! ImageLabel 1541 — 8y
Ad

Answer this question