Inv script on touch with TweenService

Hey everyone! So today I wanted to make a stage where when people step on a plank, it goes invisible so they have to be quick when they are jumping. I wanted to do it all in one script, I made a table and made the script. But I got an error saying Argument 3 missing or nil. Not really sure how to fix this. I know what the error means, I just don’t really know how to fix this. Here is my script. (Please judge my code, I need it lol, if anyone knows anyways of how to make my code not 100 lines, that would be great!)

-- Service
local TweenService = game:GetService("TweenService")
-- Variables
local PlanksTable = {
	p1 = workspace.PlanksFall.Plank1;
	p2 = workspace.PlanksFall.Plank2;
	p3 = workspace.PlanksFall.Plank3;
	p4 = workspace.PlanksFall.Plank4
}

local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)

local debounce = true

function Doinv1()
	for i = 0,0.25,1 do
		PlanksTable.p1.Transparency = i
	end
end

function Doinv2()
	for i = 0,0.25,1 do
		PlanksTable.p2.Transparency = i
	end
end

function Doinv3()
	for i = 0,0.25,1 do
		PlanksTable.p3.Transparency = i
	end
end

function Doinv4()
	for i = 0,0.25,1 do
		PlanksTable.p4.Transparency = i
	end
end

function uninv1()
	for i = 1,0.25,0 do
		PlanksTable.p1.Transparency = i
	end
end

function uninv2()
	for i = 1,0.25,0 do
		PlanksTable.p1.Transparency = i
	end
end

function uninv3()
	for i = 1,0.25,0 do
		PlanksTable.p1.Transparency = i
	end
end

function uninv4()
	for i = 1,0.25,0 do
		PlanksTable.p1.Transparency = i
	end
end

local tween1 = TweenService:Create(PlanksTable.p1,info,Doinv1())
local Tween1 = TweenService:Create(PlanksTable.p1,info,uninv1())

local tween2 = TweenService:Create(PlanksTable.p2,info,Doinv2())
local Tween2 = TweenService:Create(PlanksTable.p2,info,uninv2())

local tween3 = TweenService:Create(PlanksTable.p3,info,Doinv3())
local Tween3 = TweenService:Create(PlanksTable.p3,info,uninv3())

local tween4 = TweenService:Create(PlanksTable.p4,info,Doinv4())
local Tween4 = TweenService:Create(PlanksTable.p4,info,uninv4())

PlanksTable.p1.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce then
			debounce = false
		end
		tween1:Play()
		wait(2)
		Tween1:Play()
	end
	debounce = true
end)

PlanksTable.p1.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce then
			debounce = false
		end
		tween2:Play()
		wait(2)
		Tween2:Play()
	end
	debounce = true
end)

PlanksTable.p1.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce then
			debounce = false
		end
		tween3:Play()
		wait(2)
		Tween3:Play()
	end
	debounce = true
end)

PlanksTable.p1.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce then
			debounce = false
		end
		tween4:Play()
		wait(2)
		Tween4:Play()
	end
	debounce = true
end)

If anyone knows how to fix this, please tell me. I used a function to test things out and see if I could use it in a tween.

First of all you could take the functions such as uninv1,2,3,4 and Doinv1,2,3,4 etc and compress them into a single function that does all at once like this:

function Doinv()
	for i = 0,0.25,1 do
		PlanksTable.p1.Transparency = i
                PlanksTable.p2.Transparency = i
                PlanksTable.p3.Transparency = i
                PlanksTable.p4.Transparency = i
	end
end

You could also do this with the Touched events.

Alright, but wouldn’t that make all of the things transparent in the table?

Well you would just add a variable like PlankToInv or something, then do

function Doinv()
	for i = 0,0.25,1 do
         If PlankToInv == 1 then
              PlanksTable.p1.Transparency = i
            elseif PlankToInv == 2 then
               --you get the idea, just repeat for all planks
        end
	end
end

and just do elseifs for each variable for planks

I figured it out, I found a really simple way! Thanks!

1 Like

Here is the working script if you guys wanted to see.

local TweenService = game:GetService("TweenService")

local PlanksTable = {
	p1 = workspace.PlanksFall.Plank1;
	p2 = workspace.PlanksFall.Plank2;
	p3 = workspace.PlanksFall.Plank3;
	p4 = workspace.PlanksFall.Plank4
}

local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)

local tween1 = TweenService:Create(PlanksTable.p1,info, {Transparency = 1})
local Tween1 = TweenService:Create(PlanksTable.p1,info, {Transparency = 0})

local tween2 = TweenService:Create(PlanksTable.p2,info, {Transparency = 1})
local Tween2 = TweenService:Create(PlanksTable.p2,info, {Transparency = 0})

local tween3 = TweenService:Create(PlanksTable.p3,info, {Transparency = 1})
local Tween3 = TweenService:Create(PlanksTable.p3,info, {Transparency = 0})

local tween4 = TweenService:Create(PlanksTable.p4,info, {Transparency = 1})
local Tween4 = TweenService:Create(PlanksTable.p4,info, {Transparency = 0})

PlanksTable.p1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		tween1:Play()
		wait(2)
		Tween1:Play()
	end
end)

PlanksTable.p2.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		tween2:Play()
		wait(2)
		Tween2:Play()
	end
end)

PlanksTable.p3.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		tween3:Play()
		wait(2)
		Tween3:Play()
	end
end)

PlanksTable.p4.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		tween4:Play()
		wait(2)
		Tween4:Play()
	end
end)
1 Like