So I am making this door script, and it works, but if you get the pizza to the wrong house, (The pizza has a value which is the house number) it won’t do anything, but when you get another pizza with the house’s number than you went to on accident, it will not let you put your pizza there. The error it gives me is “PizzaBox” is not a valid member of Backpack. Here is the code.
dog = false
local cf = script.Parent.Part.CFrame
script.Parent.BoxRegion.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and dog == false then
print("found player")
dog = true
local char = workspace:WaitForChild(hit.Parent.Name)
print("found character")
local player = game.Players:GetPlayerFromCharacter(char)
print("player got from char")
local character = player.Character
print("character got from PLAYER")
if char:FindFirstChild("PizzaBox") then
print("Found pizza box")
if char.PizzaBox.HouseValue.Value == 1 or player.Backpack.PizzaBox.HouseValue.Value == 1 then
script.Parent.Part.CanCollide = false
print("turned collide off")
character.HumanoidRootPart.Position = script.Parent.GoThere.Position
character.Humanoid.WalkSpeed = 0
character.Humanoid.JumpPower = 0
end
If a player is holding a tool, that tool will be stored in their character, not their backpack.
This is wrong. If you’re trying to find the PizzaBox, you could define the variable like this: local PizzaBox = player.Character:FindFirstChild('PizzaBox') or player.Backpack:FindFirstChild('PizzaBox')
and then reference that variable when you need to access the PizzaBox.
Edit: Also try to use indents in your code to make it easier to read, thanks!
local dog = false
script.Parent.BoxRegion.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and dog == false then
dog = true
local char = player.Character
local pizzabox = char:FindFirstChild("PizzaBox") or player.Backpack:FindFirstChild("PizzaBox")
if pizzabox then
if pizzabox.HouseValue.Value == 1 then
script.Parent.Part.CanCollide = false
char.HumanoidRootPart.Position = script.Parent.GoThere.Position
char.Humanoid.WalkSpeed = 0
char.Humanoid.JumpPower = 0
end
end
end
end)
No it does not, I don’t want to sound rude but; FindFirstChild() can return a true or false. Since it’s checking for both using this it will not break the script