Been trying to turn this fading-stairs script into a client-based one

I’ve been trying to turn this script into a client-based one so I can prevent people from trolling each other in my obby. Could anybody help me out?

local block = script.Parent
db = false

function onTouch()
	if db == false then
		db = true
		for i = 1, 20 do
			block.Transparency = i/20
			wait(0.05)
		end
		block.CanCollide = false
		wait(2)
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:connect(onTouch)
7 Likes

Just pass it to a LocalScript and add a conditional (if check) to require the touching part to be part of the LocalPlayer’s character

2 Likes

Alright I’ll change it to a LocalScript, though I really don’t know about the last part since it was from a tutorial and I’m fairly new to coding. How and where would I put the conditional segment?

1 Like

Replace this line

function onTouch()

With this

function onTouch(part)

And put this line directly underneath

if part.Parent ~= game.Players.LocalPlayer.Character then return end

That will basically stop running the code if another user steps on it. Checking that it was actually you.

2 Likes

if db == false then

Do i remove this line or keep it?

1 Like

Keep it. That line is making it so you can’t trigger it while it’s already running which would break things.

2 Likes

Alright mane much appreciated, have a blessed day/night and I thank y’all both for the help💯

2 Likes

Hate to bother, and I know I’ve already marked this as a solution. but I just tested the script in-game and the parts just don’t disappear at all now. There are no errors in the dev console, so could you or anyone else let me know if I did something wrong? Here’s the script, and the script is inside all individual parts as local scripts:

local block = script.Parent
db = false

function onTouch(part)
	if part.Parent ~= game.Players.LocalPlayer.Character then return end
	if db == false then
		db = true
		for i = 1, 20 do
			block.Transparency = i/20
			wait(0.05)
		end
		block.CanCollide = false
		wait(2)
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:connect(onTouch),

Here’s a clip:

1 Like

I can’t watch the video, but I don’t think that local scripts run in workspace usually. You can however put it in a script and set the RunContext property to ‘local’ or ‘client’ or whatever. That will make it look like a local script, but run in workspace.

1 Like

Would you mind explaining how I would do that, and will I have to change every single local script back into a script?

You would probably have to change every single script back, also I noticed some parts that could be worked on in your code for the future.

        for i = 1, 20 do
			block.Transparency = i/20
			wait(0.05)
		end

Instead of using a for loop to change the transparency try using a tween instead, it will make it more smooth and polished.

wait(2)

Instead of wait try to use task.wait() as its more predictable and better in general.

And instead of making a script for every single stair, make a module, or assign a tag to the stair using CollectionService, and just make the script checks if it has that tag and make the stairs disappear.

This is how I would do it, but you do you I won’t stop you

2 Likes

Redoing all the local scripts into regular scripts now, so should they look like this? And I really don’t know how to implement the tween thing:

local block = script.Parent
db = false

function onTouch(part)
	if part.Parent ~= game.Players.LocalPlayer.Character then return end
	if db == false then
		db = true
		for i = 1, 20 do
			block.Transparency = i/20
			task.wait(0.05)
		end
		block.CanCollide = false
		task.wait(2)
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:connect(onTouch)

Yeah they should look like that, Also look up some tweening guides on YouTube they will help you understand.

1 Like

Appreciate it mane, I’m gonna change these and get some rest. Have a good one. :100:

Edit: It still doesn’t work, and that’s totally fine because I’ve come to the conclusion that maybe every obby doesn’t need a set of disappearing stairs. Much appreciation for the help though.

just create a script and set RunContext to Client, thats how you get scripts in workspace to run clientside

Yo Frost I’ve never thought about that because I’m a beginner. And I didn’t wanna touch anything because I thought something would mess up if I did. RunContext is in the properties tab right? And which of the scripts should I use? Should it be the original:

local block = script.Parent
db = false

function onTouch()
	if db == false then
		db = true
		for i = 1, 20 do
			block.Transparency = i/20
			wait(0.05)
		end
		block.CanCollide = false
		wait(2)
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:connect(onTouch)

Or the FusionOak one?:

local block = script.Parent
db = false

function onTouch(part)
	if part.Parent ~= game.Players.LocalPlayer.Character then return end
	if db == false then
		db = true
		for i = 1, 20 do
			block.Transparency = i/20
			task.wait(0.05)
		end
		block.CanCollide = false
		task.wait(2)
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:connect(onTouch)

Here’s my version of your code:

local TweenService = game:GetService("TweenService") --//smoother change in transparency

local block = script.Parent --//dont put a script in every single part its bad practice(use collectionservice for every part)


local db = false --//make sure there is a local here(putting local defines its scope and it increases optimization)


local function onTouch() --// use local function(better practice)
	if not db then --// you can say if not db
		db = true
		local t = TweenService:Create(block,TweenInfo.new(1),{Transparency = 1}):Play()
		t.Completed:Wait()
		block.CanCollide = false
		task.wait(2) --// use the task library
		block.CanCollide = true
		block.Transparency = 0
		db = false
	end
end

block.Touched:Connect(onTouch)
1 Like

Thank you for the TweenService script, though I’m not familiar with implementing CollectionService since I’m a beginner. I’m reading about it on the roblox creator hub reference page right now and it’s confusing. How would I attach every part to the single script?

I recommend you try out researching and finding tutorials. There are tons of stuff on youtube.

1 Like

Much appreciated, have a lovely evening and God bless. :sunglasses:

1 Like