Why is my code not working?

So basically im working on a teamwork obby game, and im trying to make a button to make a path appear and disappear. But for some reason it doesnt work?

I don’t know how to fix it or what to do, heres the code:

local part = script.Parent
local touching = script.Parent.Parent.Touching
local Path = workspace.TestPath
local button = script.Parent.Parent.Button_top
local tweenserivce = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
local ismoving = false

local initialSize = button.Size
local initialPosition = button.Position
local targetHeight = initialSize.Y * 0.25

local function onAnimationComplete()
	ismoving = false
end

part.Touched:Connect(function(hit)
	local character= nil
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
	local character = humanoid.Parent
	end
	
	if ismoving then print("moving, skipping...") return end
	ismoving = true
	print("Touched event triggered")

	local targetPosition = initialPosition - Vector3.new(0, initialSize.Y / 2, 0)
	local targetSize = Vector3.new(initialSize.X, targetHeight, initialSize.Z)

	local InPosition = tweenserivce:Create(button, tweeninfo, {Position = targetPosition})
	local InSize = tweenserivce:Create(button, tweeninfo, {Size = targetSize})
	local path = tweenserivce:Create(Path, tweeninfo, {Transparency = 0})

	path.Completed:Connect(onAnimationComplete)
	InPosition.Completed:Connect(onAnimationComplete)
	InSize.Completed:Connect(onAnimationComplete)

	path:Play()
	InPosition:Play()
	InSize:Play()
	Path.CanCollide = true
	
	wait(.3)
	if character then
		print("Still touching")
	elseif character == nil then
		ismoving = true
		print("TouchEnded event triggered")

		local outPosition = tweenserivce:Create(button, tweeninfo, {Position = initialPosition})
		local outSize = tweenserivce:Create(button, tweeninfo, {Size = initialSize})
		local path2 = tweenserivce:Create(Path, tweeninfo, {Transparency = 1})

		outPosition.Completed:Connect(onAnimationComplete)
		outSize.Completed:Connect(onAnimationComplete)

		outPosition:Play()
		outSize:Play()
		path2:Play()
		Path.CanCollide = false
	end
end)

part.TouchEnded:Connect(function()
	if ismoving then print("moving, skipping...") return end
	ismoving = true
	print("TouchEnded event triggered")

	local outPosition = tweenserivce:Create(button, tweeninfo, {Position = initialPosition})
	local outSize = tweenserivce:Create(button, tweeninfo, {Size = initialSize})
	local path2 = tweenserivce:Create(Path, tweeninfo, {Transparency = 1})

	outPosition.Completed:Connect(onAnimationComplete)
	outSize.Completed:Connect(onAnimationComplete)
	
	outPosition:Play()
	outSize:Play()
	path2:Play()
	Path.CanCollide = false
end)

Although it may not be the only error, an error is definitely present in this:

local character= nil
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
    local character = humanoid.Parent
end

Within the if statement, you are creating a new variable called character, instead of updating the already existing variable.
Removing the local within the if statement may resolve your issue

i.e.

local character= nil
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
    character = humanoid.Parent
end

The above SHOULD address the issue you are having. Be adviced that there is a logical error present within your code as well. Later on, you use

if character then

All this does is check if the character still exists, not whether it is still touching the part. Consider using :GetTouchingParts or :GetPartBoundsInBox to get if the player is still touching the part.


(Also, you misspelt tweenservice, and it’s annoying me :slight_smile: )

my troubleshooting tips:

1. Check the object hierarchy:

local part = script.Parent

Ensure that part is referring to the correct button object.

2. Verify variable assignments:

local touching = script.Parent.Parent.Touching
local Path = workspace.TestPath
local button = script.Parent.Parent.Button_top

Double-check the variable assignments for touching, Path, and button. Make sure they are correctly assigned to the respective objects in your game.

3. Check for errors:

print("Run the script and check the output or console for any error messages.")

4. Confirm if the button is being touched:

part.Touched:Connect(function(hit)
    -- Add print statements to check if the events are being triggered.
    print("Touched event triggered")
end)

part.TouchEnded:Connect(function()
    -- Add print statements to check if the events are being triggered.
    print("TouchEnded event triggered")
end)

Add print statements within the Touched and TouchEnded event handlers to check if they are being triggered when the button is touched or released.

5. Confirm if the part.TouchEnded event is necessary:

-- Consider removing this event if it's not necessary for your game's requirements.
part.TouchEnded:Connect(function()
    -- Add print statements to check if the event is being triggered.
    print("TouchEnded event triggered")
    -- Rest of the code for handling touch ended event...
end)

Consider removing the part.TouchEnded event if it’s not necessary for your game.


and if my didn’t work, and you tried my solutions, I also asked an paid ai to see if that helps:

Analysis of the provided code

Possible Bugs

  1. The first possible bug in the given code is that the variable tweenserivce is misspelled. It should be tweenservice. This will result in a NameError when trying to access the TweenService module. To fix this, we need to correct the spelling of the variable. For example, local tweenservice = game:GetService("TweenService").
  2. The second possible bug is that the variable Path is not defined before it is used. This will result in a NameError. To fix this, we need to define Path before using it. For example, local Path = workspace.TestPath.
  3. The third possible bug is that the variable ismoving is not initialized before it is used. This will result in a nil value when trying to check its initial state. To fix this, we need to initialize ismoving to false before using it. For example, local ismoving = false.
  4. The fourth possible bug is that the variable character is defined twice within the same scope. This will result in a redeclaration error. To fix this, we need to remove the second declaration of character within the if statement. For example, remove the line local character = humanoid.Parent.
  5. The fifth possible bug is that the TouchEnded event is not disconnected when the script is stopped or destroyed. This can lead to unexpected behavior or memory leaks. To fix this, we need to disconnect the TouchEnded event when it is no longer needed. For example, part.TouchEnded:Disconnect().

Possible Optimizations

  1. The code can be optimized by using local variables for frequently accessed properties or values. For example, instead of accessing initialSize.Y and initialSize.X multiple times, we can assign them to local variables and use those variables instead.
  2. The code can be optimized by using a single TweenService:Create call instead of creating separate tweens for Position, Size, and Transparency. This can reduce the number of function calls and improve performance.

Additional Room for Improvement

  1. The code can be improved by adding error handling for the TweenService:Create calls. If any of the tweens fail to create, it would be helpful to handle the error gracefully and provide appropriate feedback or fallback behavior.
  2. The code can be improved by organizing the code into separate functions or modules to improve readability and maintainability.

Summary

In summary, the given code had several possible bugs, including misspelled variable names, undefined variables, uninitialized variables, redeclaration of variables, and missing event disconnections. These bugs can be fixed by correcting the variable names, defining the variables, initializing the variables, removing the redeclaration, and disconnecting the events when necessary. The code can be further optimized by using local variables, combining tweens, and adding error handling. Additionally, organizing the code into functions or modules can improve readability and maintainability.

References

Now that fixed a little, there is still a problem of the button going up and down repeatedly while im just standing still shown in the video.
Now there is no errors, no spelling mistakes that matter (except for the tweenservice but it doesnt change the code).
I don’t understand why the code would do so, I have tried to make many different measures against the issue, but it seems like every attempt isnt working. At this point I think its more of robloxes fault instead of mine.