For my obby, I’m trying to make a disappearing staircase. And I’m just currently making a part and the script for it and it does not work. Here is my code:
function onTouched(hit, player)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
repeat wait(0.3)
script.Parent.Transparency += 0.1
until script.Parent.Transparency == 1
if script.Parent.Transparency == 1 then
script.Parent.CanCollide = false
wait(2)
repeat wait(0.3)
script.Parent.Transparency -= 0.1
until script.Parent.Transparency == 0
script.Parent.CanCollide = true
end
end
end
script.Parent.Touched:Connect(onTouched)
The error I get when I step on it:
Workspace.Part.Script:2: attempt to index nil with ‘Name’
Please if you could help me, I’d greatly appreciate it.
The person above me is correct but to sum up your script would look like this:
function onTouched(hit, player)
if hit.Parent:FindFirstChild("Humanoid") then
repeat wait(0.3)
script.Parent.Transparency += 0.1
until script.Parent.Transparency == 1
if script.Parent.Transparency == 1 then
script.Parent.CanCollide = false
wait(2)
repeat wait(0.3)
script.Parent.Transparency -= 0.1
until script.Parent.Transparency == 0
script.Parent.CanCollide = true
end
end
end
script.Parent.Touched:Connect(onTouched)
This should work, as the script which the person commented set the brick to cancollide true after making the brick disappear, than setting it to false.
Edit, found the problem, it seems like the function runs over and over when jumping on a brick, setting a var to false at the start of the problem, and then setting it to true at the end of the function, and having it be true to run should fix the problem.
function onTouched(hit, player)
local player = game.Players:GetPlayetFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and player then
repeat wait(0.3)
script.Parent.Transparency += 0.1
until script.Parent.Transparency == 1
if script.Parent.Transparency == 1 then
script.Parent.CanCollide = false
wait(2)
repeat wait(0.3)
script.Parent.Transparency -= 0.1
until script.Parent.Transparency == 0
script.Parent.CanCollide = true
end
end
end
Use local functions since they are more faster because the lua interpreter know’s where it is in the stack There is no need of a repeat loop, you can use a for loop.
Demonstration:
for i = 0, 1, .1 do -- First is the starting number, second is the ending number, third is the incrementing number
part.Transparency = i
wait(.2)
end
Once the for loop has completed, set the part’s CanCollide to false.
for i = 0, 1, .1 do -- First is the starting number, second is the ending number, third is the incrementing number
part.Transparency = i
wait(.2)
end
part.CanCollide = false
I’m not writing a full script for you since spoon feeding isn’t the way to learn.
That is just a demonstration on what OP is wants to do. He never mentioned he wanted to tween, though it will be more smoother. Since Tween runs on Heartbeat, he can wrap the for loop in Heartbeat and it’ll be more smoother.
You can put this Script inside the part and it should work:
local part = script.Parent;
local alreadyTouched = false;
local function on_Touch(hit)
if hit.Parent.Humanoid and game.Players:GetPlayerFromCharacter(hit.Parent) and not alreadyTouched then
alreadyTouched = true
local tweenService = game:GetService("TweenService");
local tweenInfo = TweenInfo.new(2);
local goal = {
Transparency = 1;
}
local finalTween = tweenService:Create(part, tweenInfo, goal);
finalTween:Play()
repeat wait()
until finalTween.Completed:Wait()
part.CanCollide = false
local finalTween2 = tweenService:Create(part, tweenInfo, {Transparency = 0});
finalTween2:Play()
repeat wait()
until finalTween2.Completed:Wait()
part.CanCollide = true
alreadyTouched = false
else
return
end
end;
part.Touched:Connect(on_Touch)
local r = true
script.Parent.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") and r == true then
r = false
repeat script.Parent.Transparency += 0.1 wait(0.1) until script.Parent.Transparency >= 1
script.Parent.CanCollide = false
wait(2)
repeat script.Parent.Transparency += -0.1 wait(0.1) until script.Parent.Transparency <= 0
script.Parent.CanCollide = true
r = true
end
end)
He can take this code, look onto it, and learn from it. If there is anything he needs to question about, I will surely accept him DMing (direct messaging) me and asking.
Hey there,
The reason you’re getting this error is due to you trying to access player.Name. In a .Touched connection, player does not exist, only hit. This is why it says you’re trying to index nil with “Name”.
Another think I could suggest is using TweenService, as manually changing the part’s transparency by 0.1 can cause issues (and floating point may screw up your repeat loops!)
I would also add a debounce, since it may cause issues down the road with players hitting the same part over and over again.
Here’s how I would implement this (with simpler terms):
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local Part = script.Parent
local Debounce = false
local function OnTouched(Hit)
if Hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
Debounce = true
local Tween = TweenService:Create(Part, Info, {Transparency = 1})
Tween:Play()
Tween.Completed:Wait()
Part.CanCollide = false
Tween = TweenService:Create(Part, Info, {Transparency = 0})
Tween:Play()
Tween.Completed:Wait()
Part.CanCollide = true
Debounce = false
end
end
Part.Touched:Connect(OnTouched)
You should always put a variable to make sure that your touch event doesn’t repeat as many times as it detects a hit before executing the full funcntion i use r in this example.
also you should remove that transparency check, since you’re getting it to 1 anyway.
here is your script fixed with the things i’ve told you.
local r = true
function onTouched(hit, player)
if hit.Parent:FindFirstChild("Humanoid") and r == true then
r= false
repeat wait(0.3)
script.Parent.Transparency += 0.1
until script.Parent.Transparency >= 1
script.Parent.CanCollide = false
wait(2)
repeat wait(0.3)
script.Parent.Transparency -= 0.1
until script.Parent.Transparency <= 0
script.Parent.CanCollide = true
r = true
end
end
script.Parent.Touched:Connect(onTouched)
He wants to know how to make a parts transparency degrade over time, so i mentioned tweening as that’s more smoother and more of a quicker way.
He never asked for the same method that he used, so stop shooting down valid solutions please. Afaik he hasnt even marked a solution yet. a lot of people provided the same code too.