Script Help needed!

Hi, So me and my friend are trying to make a marble game but we need help with one of the scripts. We have the script so when we click a button the marble spawns, then when you touch it, it puts you in the marble. We want to make it so only one person can get inside the marble at one time.

Here is the script we are using for the marble:

function touch(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		local stuff = hit.Parent:GetChildren()
		for i = 1,#stuff do
			if stuff[i].ClassName == "Hat" then
				stuff[i]:Destroy()
			end
		end
		game.Players:FindFirstChild(hit.Parent.Name).Character.UpperTorso.CFrame = script.Parent.CFrame + Vector3.new(0,.5,0)
		script.Parent.Parent = hit.Parent
		local w = Instance.new("Weld")
		w.Part0 = hit.Parent.UpperTorso
		w.Part1 = script.Parent
		w.Parent = hit.Parent.UpperTorso
		script.Parent.Anchored = false
		while true do
			wait()
			h.Sit = true
		end
	end
end

script.Parent.Touched:connect(touch)

And here is the issue demonstrating our issue:

We are also having an issue where if two marbles touch the turn into 1 making both players in one marble.

You need to add a variable to track whether its occupied, set that to true and false and before the script does anything it should check and exit if occupied like:

if occupied then
     return
end
occupied = true
--continue with function...

also, you might need debounce timer to keep it from rapid firing, touched is finicky

try making the player autorotate to false?
wrong reply

Its hard to see from the 3 second clip, but the problem is that a 2nd player is joining the seat while someone is already in there.

wait i just noticed that now so the solution should be the one you said, also using while true do to make the player sit isn’t a good idea