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

Need help with touched function, how do I fix the bugs?

script.Parent.CanCollide = false



script.Parent.Touched:Connect(function(hit)
	
	script.Parent.Transparency = 1
	script.Parent.Parent.PushedStandButton.Transparency = 0
	wait(0.1)
	
end)

script.Parent.TouchEnded:Connect(function(hit)
	
	script.Parent.Transparency = 0
	script.Parent.Parent.PushedStandButton.Transparency = 1
	wait(0.1)
end)

The thing I try to do is to make a platform that when you touch it, it gets transparent and when you do not touch it anymore the platform is visible again.

The script works kind of, it is just that the script is buggy when you touch the platform, when you move around on it changes from transparent to visible all the time.

Does anyone know how to fix this? I have tried a lot of things but nothing has worked for me.

Thanks, Xsodar

2 answers

✓ Accepted answer

there are two problems with this

one, you're not checking whether it's a player that's touching the platform or just a random object

to fix that you need to add an if statement to check if the thing that touched the platform was a player

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		-- code goes here
	end
end)

two, since the player's legs are constantly moving up and down, the Touched and TouchEnded events are constantly firing

there's two ways i can think of to get past that, but for simplicity's sake i'm going to share the easier of the two

you can weld an uncollidable box that covers the player's body to the player's HumanoidRootPart, and only change the transparency if the uncollidable box triggered the Touched or TouchEnded event

the other method i thought of was using raycasting, which is a bit less hacky, but that's just. a whole other can of worms

Well the reason is that anything can activate the script, not just players. That may be the reason of the lag. You could add an if then statement to fix this.

if hit.Parent:FindFirstChild("Humanoid") then

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