! | Giving a tool if conditions are met [issue]

My goal:

  1. The script checks if it’s night
  2. If true, it checks if the player has a coffee
  3. If player has coffee they get a knife
  4. If it is from 4 to 5, all coffee & knives are removed

My problem:
I don’t get a knife even if I have a coffee.

Main Script:

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() 
	game.Players.PlayerAdded:Connect(function(player)
		local coffee = player.Backpack:FindFirstChild("Coffee") or player.Character:FindFirstChild("Coffee")
		local nocoffee = player.Backpack:FindFirstChild("Coffee") and not player.Character:FindFirstChild("Coffee")
		local knife = player.Backpack:FindFirstChild("Knife") or player.Character:FindFirstChild("Knife")

		if clockTime <= 5 or clockTime >= 20 then
			if coffee then
				giveKnives()
			end
		end


		if clockTime <= 4 or clockTime >= 5 then
			if knife then
				removeKnives()
			end

			if coffee then
				repeat 
					coffee:Destroy()
				until
					nocoffee
			end
		end
		
	end)
end)
	end
	end)
end)

My functions: (these work)

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

local knife = lighting.Knife

local function giveKnives()
	for _, player in players:GetPlayers() do
		if not player.Backpack:FindFirstChild("Knife") and not player.Character:FindFirstChild("Knife") then
			local newKnife = knife:Clone()
			newKnife.Parent = player.Backpack
		end
	end
end

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

		if knifeCheck then
			knifeCheck:Destroy()
		end
	end
end

Does anybody have any ideas? Boost c:

I found an issue with your script,

local nocoffee = player.Backpack:FindFirstChild("Coffee") and not player.Character:FindFirstChild("Coffee")

which i saw was to be used in a repeat until loop condition.

this doesnt work as it stores the same value as the coffee variable, even after destruction, it would continue to not be nil, thus never breaking the loop and never giving you the knife.

what you should really do is

			if coffee then
				repeat 
                    local coffeeToDestroy = player.Backpack:FindFirstChild("Coffee") or player.Character:FindFirstChild("Coffee")
					coffeeToDestroy:Destroy()
				until
					not player.Backpack:FindFirstChild("Coffee") and not player.Character:FindFirstChild("Coffee")
			end

(i assumed there was multiple coffees that could be in your inventory and kept the repeat loop)

1 Like

Thank you, I appreciate the response! The script removed the one error that was appearing in my output, but the same issue of not getting a knife persists. :C

The code is not repeated or have a trigger, this code only runs upon the player joining (which is probably why it isnt giving you it)

also i hope you are aware that Lighting.ClockTime is in seconds (60*60*24 ClockTime to complete an entire day)

1 Like
  1. The line of code above doesnt really make sense to me, why is this here
  1. As some have stated, this function would only run when the players join the game. Putting this under the property changed function doesnt really make sense (to me atleast, im not sure what you are trying to achieve)

What i would do is figure out a way to detect when the players need a coffee/knife, and give it to them.

For example, you can have boolValues stored in the player, and if the player has a coffee that value is turned true. You can have a property changed function to give said player a knife. This is only an example because im unsure what you are tryibng to achieve.

I hope this helps!

1 Like
  1. The first one was to run the code everytime the clocktime was changed! I don’t know if it is uneeded since I’m pretty new to Roblox scripting, so I’ll remove that if so!

  2. The second one was to reference the player! I wasn’t sure if doing something like local player = game:GetService("Players") would be better so I did that. Not sure, do you happen to have any suggestions as to how I reference the player?

  3. Oh, thank you! That is smart and makes sense, I will try that tonight! :smiley:
    I’m wondering though, are you sure a boolvalue is necessary? Don’t the checks do the same thing?

1 Like

Ohh, okay! How do you suggest I trigger it? Should I remove the playeradded function?

remove the playeradded function and make it a game:GetService("Players"):GetPlayers() for loop instead

1 Like

Well to reference the player directly, you can use

local plr = game.Players.LocalPlayer

this method ONLY works for local scripts, on serversided scripts you can:

  1. Grab the player via remote event (Like, lets say you want to give a knife to the player or a coffee, you can fire a remote event on the client and get the player through the arguments in the server
  2. You can also use the game.Players:GetPlayerFromCharacter() function, but you need the players character to get the player.

What i do is i usually work with remote events/functions so i find it easier to grab the player using those. If you need any help on where to start on remote events or functions feel free to ask!

hope this helps :slightly_smiling_face:

Oh? How should I say 5 & 20, then?