What am I doing wrong with this script?

Hello everyone,

So I am trying to script this block so that when you touch it it will go transparent over a while then when the transparent and it returns after it is at fully transparent (code not finshed yet). I am unsure what is wrong but I believe it is the transparency if stament.

local part = script.Parent

part.Touched:Connect(function(plr)
local player = plr.Parent:FindFirstChild(“Humanoid”)

if player ~= nil then
	print("Human")
	while true do
		print("Pressed!")
		part.Transparency = part.Transparency + 0.1
		wait(2)
		if part.Transparency == 1 then
			part.Transparency = 0
		break
		end
	end
end

end)

2 Likes

What you’re trying to do is inefficient when there’s a better method, like TweenService.

Here’s what a demo of this would look like:

--ts is TweenService
local ti = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local toTransparent = ts:Create(workspace.SomeObject, ti, {Transparency = 1})

toTransparent:Play()

--detect when it finishes the tween:
toTransparent.Completed:Connect(function()

    part.SomeObject.Transparency = 0
    toTransparent:Play()

end)

I recommend you read further with the link I provided above if you’re still unsure.

3 Likes

Do you want it transparent or CanCollide off?

1 Like

Transparent then can collide off (in the script I have said it is not finshed).

1 Like

Getting you the scripts, 1 sec.

1 Like

Use TweenService like @TheCarbyneUniverse said