Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help with Touched function?

What this script is suppose to do is when you sit down in the vehicle seat then you are suppose to have your body's transparency to == 1 then when you get up off the seat you will have your transparency to = 0

script.Parent.Touched:connect(function(hit)
	 if hit.Parent.Humanoid.Sit == true then
	 	for i, p in pairs(hit.Parent:GetChildren()) do
			p.Transparency = 1
	 	end
	end
	 if hit.Parent.Humanoid.Sit == false then
		for i, p in pairs(hit.Parent:GetChildren()) do
	    	p.Transparency = 0
	 	end
	end
end)

2 comments

0

Did you try using a While loop that detects if the player is Sitting? Break the loop once he gets up and set the Transparency back after the loop

0

nope

1 answer

Ok, well first of all, you haven't checked to see if humanoid is actually existent nor whether the player exists. Also, in the for loop you haven't checked to see if p is a part. These could all lead to problems, for example if any part touched that, the script would be broken for the duration of the time that server's up, simply because the script returned an error because it was told that the part had a humanoid with it didn't! Here's the fixed code:

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and game.Players:FindFirstChild(hit.Parent.Name) ~= nil then -- Check if there's a humanoid and the player exists.
		if hit.Parent.Humanoid.Sit == true then
			for i, p in pairs(hit.Parent:GetChildren()) do
				if p:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then -- Check if it's a part and isn't the humanoid root part.
					p.Transparency = 1
				end
			end
		end
		if hit.Parent.Humanoid.Sit == false then
			for i, p in pairs(hit.Parent:GetChildren()) do
				if p:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then -- Check if it's a part and isn't the humanoid root part.
					p.Transparency = 0
				end
			end
		end
	end
end)

If this works for you or helps you to understand please vote up and accept this as your answer.

2 comments

0

Does not.

0

Who upvoted it, it doesn't work .-.

This discussion is preserved as it was. New answers, comments, and votes are closed.