Instances won't get added to player?

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

Are you testing this in Studio?


Since Studio is built differently, the PlayerAdded event fires before the server reads the script, so it will not work sometimes.


Try testing in the live game, to see if it works everytime.

1 Like

Hey! Thanks for the suggestion! I’ll try and see what happens!

Nope, no luck at all.

Tried this in live game, didn’t got added.

I am guessing this is a server script correct? Ok first things first, whenever there is a code problem, print the terms to see what you are dealing with. So for the first one, after the block of code runs, print the instance itself and the parent, and see what you find! Then come back and tell us!

1 Like

I already tried it, it prints absolutely nothing.

1 Like

Ok good you were already one step ahead of me. I use that kind of function for adding stats for players all the time, but I write my functions where it will call at the end. Like

Function addPlayer(player)

end
game.Players.PlayerAdded:connect(addPlayer)

It works for me every time, so it is worth a try, until I think of something else.
Sorry it is taking me so long to write, I am on an iPad!

1 Like

Thanks for the try but sadly didn’t work…

1 Like

How many times does it not work vs work? Do notice anything different when it does or doesn’t?

Okay so um, I somehow fixed it. I did how you told me to add a function and call it.

Well I added that to the PlayerAdded event and added a for loop.

The rest part of my code:

game.Players.PlayerAdded:Connect(PlayerAdded)

for _, v in ipairs(game.Players:GetPlayers())do
	PlayerAdded(v)
end

But thanks for half the solution!

Edit 1: It now prints everything!

1 Like