local KeyData = require(script.Parent.DataSettings)
local DSS = game:GetService("DataStoreService")
local Class = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Class")
local new = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-TEST123456")
game.ReplicatedStorage.ClassChange.OnServerEvent:Connect(function(player, Class, localplayer)
if Class == "Duck Warrior" then
localplayer.PlayerData.Class.Value = "Duck Warrior"
Class:SetAsync(localplayer.UserId, localplayer.PlayerData.Class.Value)
localplayer.PlayerData.NewPlr.Value = 1
new:SetAsync(localplayer.UserId, localplayer.PlayerData.NewPlr.Value)
elseif Class == "Ranger Duck" then
localplayer.PlayerData.Class.Value = "Ranger Duck"
Class:SetAsync(localplayer.UserId, localplayer.PlayerData.Class.Value)
localplayer.PlayerData.NewPlr.Value = 1
new:SetAsync(localplayer.UserId, localplayer.PlayerData.NewPlr.Value)
elseif Class == "Healer Meduck" then
localplayer.PlayerData.Class.Value = "Healer Meduck"
Class:SetAsync(localplayer.UserId, localplayer.PlayerData.Class.Value)
localplayer.PlayerData.NewPlr.Value = 1
new:SetAsync(localplayer.UserId, localplayer.PlayerData.NewPlr.Value)
elseif Class == "Tanky Ducc" then
localplayer.PlayerData.Class.Value = "Tanky Ducc"
Class:SetAsync(localplayer.UserId, localplayer.PlayerData.Class.Value)
localplayer.PlayerData.NewPlr.Value = 1
new:SetAsync(localplayer.UserId, localplayer.PlayerData.NewPlr.Value)
end
end)
This is a server script. localplayer is a parameter of the localplayer fired from the client script. I have no idea why but when it tries to set a value to the class (the parameter) it errors in the output with an attempt to call a nil value. Datastore script:
local KeyData = require(script.Parent.DataSettings)
local DSS = game:GetService("DataStoreService")
local Coins = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Coins")
local Kills = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Kills")
local Level = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Level")
local Class = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Class")
local Episode = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-Episode")
local xpReq = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-xpReq")
local xpProg = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-xpProg")
local new = DSS:GetDataStore(KeyData.Key.."-"..KeyData.Version.."-NEWTEST456")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local plrdata = Instance.new("Folder")
plrdata.Name = "PlayerData"
plrdata.Parent = player
local coindata = Instance.new("IntValue")
coindata.Name = "Coins"
coindata.Value = Coins:GetAsync(player.UserId) or 500
coindata.Parent = leaderstats
local killdata = Instance.new("IntValue")
killdata.Name = "Kills"
killdata.Value = Kills:GetAsync(player.UserId) or 0
killdata.Parent = leaderstats
local leveldata = Instance.new("IntValue")
leveldata.Name = "Level"
leveldata.Value = Level:GetAsync(player.UserId) or 0
leveldata.Parent = leaderstats
local classdata = Instance.new("StringValue")
classdata.Name = "Class"
classdata.Value = Class:GetAsync(player.UserId) or "Duck Warrior"
classdata.Parent = plrdata
local episodedata = Instance.new("NumberValue")
episodedata.Name = "Episode"
episodedata.Value = Episode:GetAsync(player.UserId) or 0
episodedata.Parent = plrdata
local xpreqdata = Instance.new("NumberValue")
xpreqdata.Name = "xpReq"
xpreqdata.Value = xpReq:GetAsync(player.UserId) or 1000
xpreqdata.Parent = plrdata
local xpprogdata = Instance.new("NumberValue")
xpprogdata.Name = "xpProg"
xpprogdata.Value = xpProg:GetAsync(player.UserId) or 0
xpprogdata.Parent = plrdata
local NPlayer = Instance.new("NumberValue")
NPlayer.Name = "NewPlr"
NPlayer.Parent = plrdata
if new:GetAsync(player.UserId) == nil then
print("New player")
game.ReplicatedStorage.NewPlr:FireClient(player)
end
game.Players.PlayerRemoving:Connect(function(player)
Coins:SetAsync(player.UserId, coindata.Value)
Kills:SetAsync(player.UserId, killdata.Value)
Level:SetAsync(player.UserId, leveldata.Value)
Class:SetAsync(player.UserId, classdata.Value)
Episode:SetAsync(player.UserId, episodedata.Value)
xpReq:SetAsync(player.UserId, xpreqdata.Value)
xpProg:SetAsync(player.UserId, xpprogdata.Value)
end)
while wait(15) do
Coins:SetAsync(player.UserId, coindata.Value)
Kills:SetAsync(player.UserId, killdata.Value)
Level:SetAsync(player.UserId, leveldata.Value)
Class:SetAsync(player.UserId, classdata.Value)
Episode:SetAsync(player.UserId, episodedata.Value)
xpReq:SetAsync(player.UserId, xpreqdata.Value)
xpProg:SetAsync(player.UserId, xpprogdata.Value)
end
end)
Client script:
local GUI = script.Parent
local BtnFolder = GUI.Buttons
local QuestsBtn = BtnFolder.Quests
local BackpackBtn = BtnFolder.Backpack
local ProgressBtn = BtnFolder.Progress
local localplayer = game.Players.LocalPlayer
local CCB = GUI.ChooseClass.Buttons
game:GetService("ReplicatedStorage").NewPlr.OnClientEvent:Connect(function(player)
GUI.ChooseClass.Visible = true
end)
CCB.Warrior.MouseButton1Click:Connect(function()
game.ReplicatedStorage.ClassChange:FireServer("Duck Warrior", localplayer)
end)
CCB.Ranger.MouseButton1Click:Connect(function()
game.ReplicatedStorage.ClassChange:FireServer("Ranger Duck", localplayer)
end)
CCB.Medic.MouseButton1Click:Connect(function()
game.ReplicatedStorage.ClassChange:FireServer("Healer Meduck", localplayer)
end)
CCB.Tank.MouseButton1Click:Connect(function()
game.ReplicatedStorage.ClassChange:FireServer("Tanky Ducc", localplayer)
end)
These scripts are just for reference and to easily help debug on what's going on. What it's supposed to do is when the player selects their class, it fires a server event. The server receives it, sets the player's class and sets the newplayer value (to indicate that it is not a new player anymore) and then saves both values. Instead, it just calls a nil value on :SetAsync for the class value and possibly the newplr value?
Edit: I just realised I made a really small mistake, I set both the class datastore and the class parameter as the same name. Changing either of their names fixed everything.
