2 local statements makes it just skip the first one

i forget if its called statements or not sorry if the titles wrong haha

I’m making a silly cart ride game and I’ve been stuck on a part of a script with a local statement (line marked with comment). If I just keep one string then both respective seats work, but together they don’t. With the way it is currently, it only works if I get into the seat named Seat and kills me (which is supposed to happen) but if I get into the seat named Safe, it doesn’t and instead prints "no person in seat". The cart has to have 1-2 players in it.

I’ve also tried local seat = hit.Parent:WaitForChild("Safe") or hit.Parent:WaitForChild("Seat") or (hit.Parent:WaitForChild("Safe") and hit.Parent:WaitForChild("Seat")) but that didn’t work as well.

local debouce = false
local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Cart" then
		if not debouce then
			debouce = true
			local seat = hit.Parent:WaitForChild("Safe") and hit.Parent:WaitForChild("Seat") --only checks "Seat" and not "Safe"
			
			if seat.Occupant then
				local player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
				seat:SetAttribute("Player", player.UserId)
				if seat.Occupant.Parent:WaitForChild("DidYouFindTheChildOrNot").Value == false then
					seat.Occupant.Health = 0
				else
					seat.Occupant.Jump = true
				end
			else
				if not seat.Occupant then
					print("no person in seat") --prints when im sitting in "Safe" seat ??
				end
			end
			task.wait(8)
			debouce = false
		end
	end
end)

I dont think you can have an and when setting a variable, why? Cause that would make a variable two things, which isnt exactly possible.

Depending on what Safe is you might have to do different things.

If safe is something, that if its there, then you dont want to do the seat.Occupant thing, just do:
if seat.Occupant and not Safe then

So yeah

Safe and Seat are both seats. 2 players can sit in one cart, or only one player can sit in one of the two seats. So safe isn’t really just something i guess idk lul!

But I get the and with variables thing, I’ll see what I can do with that

1 Like

Yeah, so what happens with the and is that because is it an operator, it is guaranteed to evaluate into an expression (which is to say it has a value). That value is the right-hand side operand if and only if the left-hand side isn’t false or nil (which is the case, because hit.Parent:WaitForChild("Safe") exists).

Anyway, I think you’d have to implement the logic for the two seats separately. No working around that (I assume that’s what you wanted to do??)

Yeah, I was just trying to make the script less complicated and wanted to see if there was an actual solution but this worked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.