! | Referencing a BoolValue in Seperate Functions

My script is not working. Ideally, it should give a knife if the boolValue is false and if it is nighttime. It should remove the knife from 4-5 if the player has one.

I’m assuming the issue is with the value. So, how should I reference the BoolValue since they are in separate functions?


-- // create the boolvalue // --!

game.Players.PlayerAdded:Connect(function(player)
	local hasKnife = Instance.new("BoolValue")
	hasKnife.Name = "hasKnife"
	hasKnife.Parent = player
	hasKnife.Value = false
end)

--------------------------------------------------

-- // give knives if conditions are met // --!


local lighting = game:GetService("Lighting")
local clockTime = lighting.ClockTime
local player = game:GetService("Players"):GetPlayers()

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() 
		local player = game:GetService("Players"):GetPlayers() 
		local hasKnife = player:WaitForChild("hasKnife")
	
	hasKnife:GetPropertyChangedSignal("hasKnife"):Connect(function()
		if player:WaitForChild("hasKnife").Value == false then
			if clockTime <= 5 or clockTime >= 20 then
				giveKnives()
				hasKnife.Value = true
			end
		end
		
		if clockTime <= 4 or clockTime >= 5 then
			if hasKnife.Value == true then
				removeKnives()
				hasKnife.Value = false
			end
		end
	end)
end)

Also, if anyone has a better way to write my script, sharing it would be appreciated. THank you!

1 Like

You are using the :GetPropertyChangedSignal() on “hasKnife”, but you are also defining the property as hasKnife, it should be value.


-- // create the boolvalue // --!

game.Players.PlayerAdded:Connect(function(player)
	local hasKnife = Instance.new("BoolValue")
	hasKnife.Name = "hasKnife"
	hasKnife.Parent = player
	hasKnife.Value = false
end)

--------------------------------------------------

-- // give knives if conditions are met // --!


local lighting = game:GetService("Lighting")
local clockTime = lighting.ClockTime
local player = game:GetService("Players"):GetPlayers()

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() 
	local player = game:GetService("Players") -- define the player!!!
	local hasKnife = player:WaitForChild("hasKnife")

	hasKnife:GetPropertyChangedSignal("Value"):Connect(function()
		if player:WaitForChild("hasKnife").Value == false then
			if clockTime <= 5 or clockTime >= 20 then
				giveKnives()
				hasKnife.Value = true
			end
		end

		if clockTime <= 4 or clockTime >= 5 then
			if hasKnife.Value == true then
				removeKnives()
				hasKnife.Value = false
			end
		end
	end)
end)
1 Like

Player represents a table here. Not an individual.
You would prob have to use a PlayerAdded event with all of that.

2 Likes

Also this, didn’t notice that - good eye @Valkyrop

1 Like

Oh, okay! Should I remove the :GetPlayers() at the end?

Btw, you don’t have to reference the value of your bool value, it defaults as false. The people above is right though.

Is this a script or a local script?

It is a server script! :smiley: [3O charssss]

  local lighting = game:GetService("Lighting")
-- // create the boolvalue // --!

game.Players.PlayerAdded:Connect(function(player)
	local hasKnife = Instance.new("BoolValue")
	hasKnife.Name = "hasKnife"
	hasKnife.Parent = player
	hasKnife.Value = false
	lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
        local clockTime = lighting.ClockTime
		hasKnife:GetPropertyChangedSignal("Value"):Connect(function()
			hasKnife:GetPropertyChangedSignal("Value"):Connect(function()
				if player:WaitForChild("hasKnife").Value == false then
					if clockTime <= 5 or clockTime >= 20 then
						giveKnives()
						hasKnife.Value = true
					end
				end

				if clockTime <= 4 or clockTime >= 5 then
					if hasKnife.Value == true then
						removeKnives()
						hasKnife.Value = false
					end
				end
			end)
		end)
	end)
end)

This should work. unless there’s anything else I missed that @Valkyrop can spot aha :laughing:

2 Likes

Thank you so much for the script, I really appreciate it! Sadly, it still does not give me a knife. Here is the full script including my functions in case I may have done something wrong there! :smiley:


Functions:

local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
local knife = lighting.Knife

local function giveKnives()
	for _, player in players:GetPlayers() do
			local newKnife = knife:Clone()
			newKnife.Parent = player.Backpack
	end
end

local function removeKnives()
	for _, player in players:GetPlayers() do
		local knife = player.Backpack:FindFirstChild("Knife") or player.Character:FindFirstChild("Knife")
		knife:Destroy()
	end
end

Your script:

-- // create the boolvalue // --!

game.Players.PlayerAdded:Connect(function(player)
	local hasKnife = Instance.new("BoolValue")
	hasKnife.Name = "hasKnife"
	hasKnife.Parent = player
	
	lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		hasKnife:GetPropertyChangedSignal("Value"):Connect(function()
				if player:WaitForChild("hasKnife").Value == false then
					if clockTime <= 5 or clockTime >= 20 then
						giveKnives()
						hasKnife.Value = true
					end
				end

				if clockTime <= 4 or clockTime >= 5 then
					if hasKnife.Value == true then
						removeKnives()
						hasKnife.Value = false
					end
				end
			end)
		end)
	end)
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local knife = Lighting.Knife

local function giveKnife(Player)
	knife:Clone().Parent = Player.Backpack
	knife:Clone().Parent = Player.StarterGear
	local newKnife = knife:Clone()
	newKnife.Parent = Player.Backpack
end

local function removeKnife(Player)
	local Backpack = Player.Backpack
	for i,v in pairs (Player:GetDescendants()) do
		if v:IsA("Tool") and v.Name == "Knife" then
			v:Destroy()
		end
	end
	for i,v in pairs (Player.Character:GetDescendants()) do
		if v:IsA("Tool") and v.Name == "Knife" then
			v:Destroy()
		end
	end
end

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime <= 5 or Lighting.ClockTime >= 20 then
		for i,v in ipairs (Players:GetChildren()) do
			if not v.hasKnife.Value then
				v.hasKnife.Value = true
				giveKnife(v)
			end
		end
	elseif Lighting.ClockTime <= 4 or Lighting.ClockTime >= 5 then
		for i,v in ipairs (Players:GetChildren()) do
			if v.hasKnife.Value then
				v.hasKnife.Value = false
				removeKnife(v)
			end
		end
	end
end)

Try this out.