How do I make a Touched Function in a LocalScript?

Hi! This is supposed to work for whatever player touched this part, and it’s supposed to go invisible for them, but not other people that want to collect it. However, it isn’t going invisible for the ones that collected it? I need some help if I can get it.

  1. What do you want to achieve? What the title says. How to make a Touched Function in a LocalScript work.

  2. What is the issue? The code for it is not working. At all.

  3. What solutions have you tried so far? Tried changing it, still nothing.

Okay, so here is my current code I have.

local Leaf = script.Parent
local Players = game.Players or game:GetService("Players")
debounce = false

Leaf.Touched:Connect(function(hit)
	if hit.Parent ~= nil and Players:FindFirstChild(hit.Parent.Name) and debounce == false then
		debounce = true
		Leaf.Transparency = 1

	end
end)

How do I tweak this code for it to work?

2 Likes

I assume that the part is in the workspace.

Basically you can’t use local scripts in the workspace.

You can put the local script somewhere else and define the leaf’s location.

EDIT: In the roblox documentation there’s a list of places where a local script can be used at:

You can read more here: LocalScript | Roblox Creator Documentation

1 Like

Okay, thanks. I’ll try it and see if it works.

1 Like

Thanks. That worked. It works like a charm.

@Wizzthepixelcat @Corruptux
This isn’t true (not anymore at least)

You can have a LocalScript run in the workspace

  1. Spawn a Regular Script

  2. Change the Scripts RunContext to Client (located on the Behavior tab), this will turn the Script into a LocalScript

After that it will run on the workspace

Note: this only works with Regular Scripts

Documentation: (It leads to Disabled for some reason)

1 Like

Oh cool, I’ll check that out as well.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.