Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why aren't values being added consistently to the player?

Hey! I have a script that SHOULD add some values to the player when they join. The problem is, it's very inconsistent. Sometimes it adds them, sometimes it doesn't. Why does this happen and how can I fix this? The values in question are InGame, GainedDebris, and Debris. Code:

local DataStore2 = require(1936396537)
local newReward = 25

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player
	if player then
		print(player.Name.." has joined.")
	
		-- Values
	
		local inGame = Instance.new("BoolValue", player)
		inGame.Name = "InGame"
		inGame.Value = false
	
		local gainedDebris = Instance.new("IntValue", player)
		gainedDebris.Name = "GainedDebris"
		gainedDebris.Value = 0
		
		local debris = Instance.new("IntValue")
		debris.Name = "Debris"
		debris.Parent = player
		
		-- Datastore
		
		local debrisData = DataStore2("debris", player)
		
		local function debrisUpdate(Value)
			debris.Value = debrisData:Get(Value)
		end
		
		debrisData:OnUpdate(debrisUpdate)
		
		-- InGame
		
		player.CharacterAdded:Connect(function(character)
			character.Humanoid.Died:Connect(function()
				
				character:FindFirstChild("InGame").Value = false
				player:LoadCharacter()
			end)
		end)
			
	else
		print("No Player")
	end
end)

Edit: No errors, this is NOT for leaderstats. InGame is used in a different script to detect if a player is in a game. Debris is used to update an on-screen counter. GainedDebris is part of a complex system that I won't detail. Also, it never prints “No Player”

3 comments

0

Are there any errors, or there other script that changes the point leaderstats?

0

No errors, this is NOT for leaderstats. InGame is used in a different script to detect if a player is in a game. Debris is used to update an on-screen counter. GainedDebris is part of a complex system that I won't detail.

0

Also, even if the script doesn't add them, it also doesn't print "No Player." Very confusing indeed.

1 answer

✓ Accepted answer

I think because you stated that it "sometimes updates" was because you need to run a for loop that checks for (usually) the first player that joined, that wasn't caught by the PlayerAdded event. You also didn't need to keep calling the :Get() method in DataStore2 since the Parameter passed in OnUpdate() is the last updated value.

local DataStore2 = require(1936396537)
local newReward = 25

local function PlayerAdded( player ) 
	print ( player, 'Joined' ) 
	
	local inGame = Instance.new("BoolValue")
	inGame.Name = "InGame"
	inGame.Value = false
	inGame.Parent = player
	
	local gainedDebris = Instance.new("IntValue")
	gainedDebris.Name = "GainedDebris"
	gainedDebris.Value = 0
	gainedDebris.Parent = player
	
	local debris = Instance.new("IntValue")
	debris.Name = "Debris"
	debris.Parent = player
	
	-- Datastore
	
	local debrisData = DataStore2("debris", player)
	-- Pull or set Value to something incase it doesn't exist yet
	local debrisDataValue = debrisData:Get(0)
	
	debrisData:OnUpdate(function(SavedValue)
		debris.Value = SavedValue
	end)
		
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			-- FindFirstChild is pointless unless you plan to verify its existence. 
			-- Also, wasn't InGame parented to the player?
			inGame.Value = false
			player:LoadCharacter()
		end)
	end)	
		
	--[[ You can test with this
	debrisData:Increment(1)
	wait(3)
	debrisData:Increment(2)
	--]]
end

game.Players.PlayerAdded:Connect(PlayerAdded)

for _, player in pairs ( game.Players:GetPlayers()) do 
	PlayerAdded(player)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.