Value Checking Script not functioning as intended

I’m making a script that detects a bool value inside a folder inside of a player, and if it is true then every time they join the game it adds a specific item into their inventory. The value does save when you leave and rejoin, I have checked, its just that the code to check that and add the item does not work and I don’t know why.

Here is the code:

 local rp = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Keystone = player:FindFirstChild("KeyItems"):FindFirstChild("Keystone").Value
	if Keystone == true then
		warn("Player has keystone")
		local flashlight = rp.flashlight:Clone()
		local backpack = player:FindFirstChild("Backpack")
		flashlight.Parent = backpack
		warn("cloned in backpack")
	else
		warn("Player does not have keystone")
	end
end)

Everytime I run the script, it outputs the warning “Player does not have keystone” and I would check in the player to see if the bool value is true, and it would be true. There are no typos in the script or anything as well. I don’t know what I am doing wrong.

1 Like

maybe you set the parent of the keystone before changing the value, you should try printing the keystone value

Keystone.Parent = KeyItems

task.wait(1) -- something yielding

Keystone.Value = true


I added another print to check the value, and it would output as false, as the ss shows above. If the sample code you showed was for the data saving, I set the default value to itself, aka I said the value of it was the value of it. I don’t know if thats wrong but if I set the default to true wouldnt that mean that everyone would recieve the item no matter what even if they didn’t get the item? Because how it works is that once u pick up the item for the first time it sets the value to true and keeps it that way so that if anyone has not picked it up then it would be false. Correct me if I am wrong I am still fairly new to scripting.
Also the ss below shows the properties of the value inside the player showing as true.

1 Like

you should put in the keystone when the data has loaded and make the script to wait until it exists

local rp = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Keystone = player:FindFirstChild("KeyItems"):WaitForChild("Keystone", math.huge).Value
	if Keystone == true then
		warn("Player has keystone")
		local flashlight = rp.flashlight:Clone()
		local backpack = player:FindFirstChild("Backpack")
		flashlight.Parent = backpack
		warn("cloned in backpack")
	else
		warn("Player does not have keystone")
	end
end)

and if it still prints false, it could be a datastore issue

I used that code and it still input out as false. I messed around and I made the default value of the Keystone in the data script as true and it worked but I probably cant keep that since everyone would get the item if they joined, if I am right? Might just be a datastore issue then.

I actually got it to work, I just tweaked a couple of things and it works fine now. Thank you for your help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.