So, I had this function in my game, where the main point of it was to add a coloured fire effect to the torso of a character. However, the issue occurs when, or if a player resets their character. As the data load function is only called once, it will only do it once, but I want it to fire every time the character is added.
However, any method I tried didn’t exactly work, as this was the only way it would set it up.
local function LoadData(Player:Player)
local Success = nil
local playerdata = nil
local Attempt = 1
local LeaderStats = Instance.new("Folder")
LeaderStats.Name = "leaderstats"
LeaderStats.Parent = Player
repeat
Success, playerdata = pcall(function()
return Database:GetAsync(Player.UserId)
end)
Attempt += 1
if not Success then
warn(playerdata)
task.wait()
end
until Success or Attempt == 3
if Success then
print("Data Retrieved")
if not playerdata then
print("Noob Detected: Giving Default Data...")
playerdata = {
["Robux"] = 0,
["SelectedTowers"] = {"Rank 1: Pistol Man"},
["OwnedTowers"] = {"Rank 1: Pistol Man", "Rank 3: Sniper Man"},
["Level"] = 1,
["EXP"] = 0,
["ReqiredEXP"] = 50,
["SelectedFlameTrail"] = {},
["OwnedFlameTrails"] = {},
["Wins"] = 0
}
end
Data[Player.UserId] = playerdata
print(playerdata)
local Robux = Instance.new("IntValue")
Robux.Name = "Robux"
Robux.Value = playerdata.Robux
Robux.Parent = LeaderStats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = playerdata.Level
Level.Parent = LeaderStats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Value = playerdata.Wins
Wins.Parent = LeaderStats
if playerdata.Level == nil then
playerdata.Level = 1
playerdata.EXP = 1
playerdata.RequiredEXP = 50
end
if playerdata.OwnedFlameTrails == nil then
playerdata.OwnedFlameTrails = {}
playerdata.SelectedFlameTrail = {}
end
------------------------------------------------------------------------------------------------------------
if playerdata.SelectedFlameTrail ~= nil then -- if statement to add a fire when it finds any selected flame trail
local ShopItem = Flames[playerdata.SelectedFlameTrail[1]]
if ShopItem then
EquipSelectedFire(Player.Character:WaitForChild("Torso"), ShopItem)
end
end
------------------------------------------------------------------------------------------------------------
while task.wait() do
if playerdata.Level and playerdata.EXP and playerdata.RequiredEXP then
if playerdata.EXP >= playerdata.RequiredEXP then
playerdata.EXP = playerdata.EXP - playerdata.RequiredEXP
playerdata.Level += 1
playerdata.RequiredEXP = playerdata.Level * 25
end
else
task.wait(playerdata.Level and playerdata.EXP and playerdata.RequiredEXP)
if playerdata.EXP >= playerdata.RequiredEXP then
playerdata.EXP = playerdata.EXP - playerdata.RequiredEXP
playerdata.Level += 1
playerdata.RequiredEXP = playerdata.Level * 25
end
end
end
Data[playerdata.UserId].Robux.Changed:connect(function()
Player.leaderstats.Robux.Value = Data[playerdata.UserId].Robux
end)
Data[playerdata.UserId].Level.Changed:connect(function()
Player.leaderstats.Level.Value = Data[playerdata.UserId].Level
end)
Data[playerdata.UserId].Wins.Changed:connect(function()
Player.leaderstats.Wins.Value = Data[playerdata.UserId].Wins
end)
else
warn("Unable To get Data for Player: ", Player.UserId)
Player:Kick("Error: Data Retrieval was unsuccessful, try again for better results :P")
end
end
Players.PlayerAdded:Connect(LoadData) -- Calling the LoadData function only after a player instance is added.
It works when it’s called like so, from the above function, but still only calls the EquipSelectedFire function once.
I tried a CharacterAdded Event to make it so that it happens anytime a player spawns in, but nothing happened.
Players.PlayerAdded:Connect(function(Player)
LoadData(Player)
local PlayerData = Data[Player.UserId]
Player.CharacterAdded:Connect(function() -- Doesn't work, and no Errors occur either
if PlayerData.SelectedFlameTrail ~= nil then
local ShopItem = Flames[PlayerData.SelectedFlameTrail[1]]
if ShopItem then
EquipSelectedFire(Player.Character:WaitForChild("Torso"), ShopItem)
end
end
end)
end)
I tried very similar things with CharacterAdded events, but they never worked, and I’ve been puzzled on how to go about it. If there were a solution to this issue, I would really want to hear it.