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
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)
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 line of code above doesnt really make sense to me, why is this here
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.
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!
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?
Oh, thank you! That is smart and makes sense, I will try that tonight!
I’m wondering though, are you sure a boolvalue is necessary? Don’t the checks do the same thing?
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:
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
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!