Hey there, devs!
So, I was developing on a game, but for some reason instances won’t get added to player. Here’s my code
Code
--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// ReplicatedStorage Variables
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local RemoteEvents = Remotes:WaitForChild("Events")
local HumanoidDiedRE = RemoteEvents:WaitForChild("HumanoidDiedRE")
--// DataStore Variables
local _DataStore2 = require(1936396537) -- DataStore2
--// Main
game.Players.PlayerAdded:Connect(function(LocalPlayer)
local NonSaveInfo = Instance.new("Folder")
NonSaveInfo.Name = "NonSaveInfo"
NonSaveInfo.Parent = LocalPlayer
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = NonSaveInfo
local CurrentRank = Instance.new("IntValue")
CurrentRank.Name = "CurrentRank"
CurrentRank.Parent = NonSaveInfo
local PlayerData = Instance.new("Folder")
PlayerData.Name = "PlayerData"
PlayerData.Parent = LocalPlayer
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Parent = PlayerData
local TotalKills = Instance.new("NumberValue")
TotalKills.Name = "TotalKills"
TotalKills.Parent = PlayerData
local CashDataStore = _DataStore2("CashDataStore", LocalPlayer)
local TotalKillsDataStore = _DataStore2("TotalKillsDataStore", LocalPlayer)
local DefaultCashAmount = 150
local DefaultTotalKillsAmount = 0 -- Adding a default value to force players to have 0 total kills.
local function updateCash(updatedValue)
Cash.Value = CashDataStore:Get(updatedValue)
end
local function updateTotalKills(updatedValue)
TotalKills.Value = TotalKillsDataStore:Get(updatedValue)
end
updateCash(DefaultCashAmount)
CashDataStore:OnUpdate(updateCash)
updateTotalKills(DefaultTotalKillsAmount)
LocalPlayer.CharacterAdded:Connect(function(Character)
task.wait(5)
if Character:WaitForChild("HumanoidRootPart") ~= nil then
task.wait(1)
Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.SpawnBox.Spawns:GetChildren()[math.random(1, #game.Workspace.SpawnBox.Spawns:GetChildren())].CFrame
end
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
if Humanoid:FindFirstChild("Killer") then
HumanoidDiedRE:FireClient(LocalPlayer)
local Killer = Humanoid:FindFirstChild("Killer")
Killer:WaitForChild("NonSaveInfo"):WaitForChild("Kills").Value += 1
end
end)
end)
end)
The problem is that the instances sometimes get added, and sometimes they won’t. Thanks for any help!
- Arid