If the player has a coffee & it’s night , they get a knife.
When it is between four and five, the knife is constantly being removed + the coffee is constantly removed.
My problem:
Now whenever I click on a coffee during day, it immediately destroys itself. Probably an error w/ the elseif but I’m not sure how.
Script:
local clockTime = lighting.ClockTime
for _, player in players:GetPlayers() do
local checkCoffee = player.Backpack:FindFirstChild("Coffee") or player.Character:FindFirstChild("Coffee")
if checkCoffee then
if clockTime <= 5 or clockTime >= 20 then
giveKnives()
print("knifes")
elseif clockTime <= 4 or clockTime >= 5 then
removeKnives()
checkCoffee:Destroy()
end
end
end
end)
Errors:
This is the line it is referring to, but I don’t think that part is the issue. The script works fine without the elseif.
The error is because the player doesn’t have the coffee in their character, since it gets destroyed, the script is still looking for the coffee, which doesn’t exist anymore. If you don’t want the cofeee to destroy during the day I suggest you get rid of checkCoffee:Destroy() as that is destroying the coffee as your variable checkCoffee is referring to the coffee in the players inventory. Instead of FindFirstChild I would recommend using WaitForChild instead
use waitforrchild instead of find first child as it waits for the coffee to be added instead of continuously looking for it and giving errors. By the way mark it as a solution if it worked!
In this scenario I’d just use else. If you do need the time check, use this code:
local lighting = game:GetService("Lighting")
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
for _, player in players:GetPlayers() do
local checkCoffee = player.Backpack:FindFirstChild("Coffee") or player.Character:FindFirstChild("Coffee")
if checkCoffee then
if clockTime < 5 or clockTime > 20 then
giveKnives()
print("knifes")
elseif clockTime < 20 or clockTime > 5 then
removeKnives()
checkCoffee:Destroy()
end
end
end
end)
I tried it and it works during night, but during day when I try to get the coffee, it does not appear in my inventory. I added a print statement to see if the code is running and it is printing & running still.
Here’s my code now:
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
for _, player in players:GetPlayers() do
local checkCoffee = player.Backpack:WaitForChild("Coffee") or player.Character:WaitForChild("Coffee")
if checkCoffee then
if clockTime <= 5 or clockTime >= 20 then
giveKnives()
elseif clockTime <= 4 or clockTime >= 5 then
removeKnives()
checkCoffee:Destroy()
print("gone")
end
end
end
end)
local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
for _, player in pairs(players:GetPlayers()) do
local backpack = player:FindFirstChildWhichIsA("Backpack")
local character = player.Character
local coffee
if typeof(backpack) == 'Instance' then
coffee = backpack:FindFirstChild("Coffee")
end
if typeof(coffee) ~= 'Instance' and typeof(character) == 'Instance' then
coffee = character:FindFirstChild("Coffee")
end
if typeof(coffee) == 'Instance' then
if clockTime < 5 or clockTime > 20 then
giveKnives()
print("knifes")
elseif clockTime < 20 or clockTime > 5 then
removeKnives()
checkCoffee:Destroy()
end
end
end
end)
You’re using WaitForChild in a connection which can be quite risky.
Thank you! But the thing is, I still want to be able to click on the coffee during the day, I just want it to remove the knife & coffee from the player’s inventory after the night is over.
Ohh, thank you for this! I appreciate it! I tried the code & modified the time a bit (since I want the stuff to be cleared only through 4-5), although it still will not let me click on the coffee during morningtime. Here is what I have now from your code:
local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
for _, player in pairs(players:GetPlayers()) do
local backpack = player:FindFirstChildWhichIsA("Backpack")
local character = player.Character
local coffee
if typeof(backpack) == 'Instance' then
coffee = backpack:FindFirstChild("Coffee")
end
if typeof(coffee) ~= 'Instance' and typeof(character) == 'Instance' then
coffee = character:FindFirstChild("Coffee")
end
if typeof(coffee) == 'Instance' then
if clockTime < 5 or clockTime > 20 then
giveKnives()
print("knifes")
elseif clockTime < 4 or clockTime > 5 then
removeKnives()
coffee:Destroy()
end
end
end
end)```
Another thing I’m concerned about is the function being called. Why is it being called inside of a loop? It would then be called several times but there’s no requirement for a player argument to give the knifes to or remove the knives from.
According to the code, once it is all done it would then execute that function multiplied by the amount of people in the server, which is something I assume you don’t want.
If that’s true, then use this code & break your loop.
local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
local i = nil
local coffee
for _, player in pairs(players:GetPlayers()) do
local backpack = player:FindFirstChildWhichIsA("Backpack")
local character = player.Character
if typeof(backpack) == 'Instance' then
coffee = backpack:FindFirstChild("Coffee")
end
if typeof(coffee) ~= 'Instance' and typeof(character) == 'Instance' then
coffee = character:FindFirstChild("Coffee")
end
if typeof(coffee) == 'Instance' then
if clockTime < 5 or clockTime > 20 then
i = 0
elseif clockTime < 20 or clockTime > 5 then
i = 1
end
break
end
end
if i == 0 then
giveKnives()
elseif i == 1 then
removeKnives()
if typeof(coffee) == 'Instance' then
coffee:Destroy()
end
end
end)
Do everything that should be done stand-alone outside of the loop.
honestly I would just add a click detector to the coffee and when it is clicked you got the player as a parameter and then you do the code with waitforchild. or if your using @04nv 's code just use findfirstchild also make sure if the coffee is a tool to uncheck cantouch as automatically a tool gets equipped when player walks into it which then you wouldn’t be able to get the player
Local coffee = --where the coffee is originally placed
local click = coffee.ClickDetector
local clockTime = lighting.ClockTime
click.MouseClick:Connect(function(player)
coffee.Parent = player.Character
end)
local checkCoffee = player.Backpack:WaitForChild("Coffee") or player.Character:WaitForChild("Coffee")
if checkCoffee then
if clockTime <= 5 or clockTime >= 20 then
giveKnives()
elseif clockTime <= 4 or clockTime >= 5 then
removeKnives()
checkCoffee:Destroy()
print("gone")
end
end
here is updated version I realized I made some mistakes
do updated version I forgot to make it so the coffee gets parented to the players character @triisflame and like @04nv said its better to do waitforchild outside of a connect function and are you changing the clock time in another script? because here the clock time isnt changing so you have to have like some script that does a day and night system by changing clock times
Hi again! I’m trying to make it so that the script will remove all coffee in the inventory only one time when it is 4 to 5 AM.
Do you guys have any possible ideas on how I can make it so that the coffee is only removed one time at 4-5 AM?
Here’s the code:
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local clockTime = lighting.ClockTime
for _, player in players:GetPlayers() do
local checkCoffee = player.Backpack:WaitForChild("Coffee") or player.Character:WaitForChild("Coffee")
if checkCoffee then
if clockTime <= 5 or clockTime >= 20 then
giveKnives()
elseif clockTime <= 4 or clockTime >= 5 then
removeKnives()
end
end
end
end)