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)
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?
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),
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.
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
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)
Appreciate it mane, I’m gonna change these and get some rest. Have a good one.
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.
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)
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)
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?