Function not working, why?

I can’t seem to get this script working/running properly. It’s pretty simple but, I’m a beginner trying to do above and beyond to learn in a much better way. Can someone please help me understand why this function isn’t working?

I re-written this script 3 times, I got my friends help but didn’t understand fully.

I’m trying to make it so, every time I touch this part it will go transparent. What am I doing wrong?

	function turnsInvisible(part,newTransparency)
		part.Transparency = newTransparency
		turnsInvisible(game.Workspace.TouchMe.Touched,newTransparency.Transparency,1)
	end
end)```
1 Like

what is this code ??
please explain me what you are trying to do, that way i can help you better

this script makes no sense

I’m trying to make it so, every time I touch a certain part it will set the transparency to one. Every time I step off it, will be set back to 0.

Okay, what is this script? I dont mean to be mean but this doesnt make sense as your repeated a function inside of a new function trying to call a function.

I suggest you look into the documentation of calling and using functions.

1 Like
game.Workspace.MyPart.Touched:Connect(function(Obj)
if not Obj.Parent:FindFirstChild("Humanoid") then return end
--do code
end)

This script is pretty confusing but it looks like you’re trying to use 2 different ways of making events. What you should do is take your turnsInvisible function and move it outside of the event. Then do workspace.TouchMe.Touched:Connect(turnsInvisible) and try it again

function turnsInvisible(hit)
    hit.Transparency = 1
end

workspace.TouchMe.Touched:Connect(turnsInvisible)

Try something like this:

workspace.TouchMe.Touched:Connect(function(hit)
     if not hit.Parent:FindFirstChild("Humanoid") return end
     workspace.TouchMe.Transparency = 1
end)

workspace.TouchMe.TouchEnded:Connect(function()
     workspace.TouchMe.Transparency = 0
end)
local touchCache = {}

local part = workspace.TouchMe

local function getIndexFromElement(element, table)
    for i, v in ipairs(table) do
        if v == element then
            return i
        end
    end
end

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        part.Transparency = 1
        table.insert(touchCache, hit)
    end
end)

part.TouchEnded:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        table.remove(touchCache, getIndexFromElement(hit, touchCache))

        if #touchCache == 0 then
            part.Transparency = 0
        end
    end
end)

The touchCache will ensure that even if a part of a player stops touching the part, if there are other parts touching it, it will stay invisible.

It seems that you have some other misunderstanding when it comes to scripting. For example, you shouldn’t be defining a function in an event. I would recommend reading the tutorials on the roblox developer hub

First, you are passing a event through a function: attempting to change that transparency.
In the second argument (newTransparency), you are passing in a number, but giving it a nil property.

What you should do is change the 1st argument pass to this: game.Workspace.TouchMe
Removed the second argument: newTransparency.Transparency

Then, move the function call outside the function.
Should look like this:

game.Workspace.TouchMe.Touched:Connect(function(turnsInvisible)
   function turnsInvisible(part : Instance, newTransparency : number)
   part.Transparency = newTransparency
end
turnsInvisible(game.Workspace.TouchMe, 1)
end)

Fyi, you can just remove the “TurnsInvisible” function, and just let the already existing touched function wrap around: “part.Transparency = 1”.

(plus, you can use a variable for “game.Workspace.TouchMe” to make the code nicer.)

also, just to let it load in, use task.wait() at the start. (since you are going to the workspace, and finding “TouchMe” on game start.

task.wait()

game.Workspace.TouchMe.Touched:Connect(function(turnsInvisible)
   function turnsInvisible(part : Instance, newTransparency : number)
   part.Transparency = newTransparency
end
turnsInvisible(game.Workspace.TouchMe, 1)
end)
1 Like

Thank you, this helped me understand it a little bit more.

I did however have to modify it a little bit for it to work.

Thank you for the help!

1 Like

Thank you very much, that is a lot of information I needed.