OnTouch can't run locally?

I’m trying to make a system where if a part in ReplicatedStorage is touched, a BoolValue in a local script is set to true, and when stepped off it’s set to false. This works fine in a normal server script, but the problem is, when I put the local script it doesn’t run. The script is a child of a part in ReplicatedStorage, so why can’t it run locally? How could I fix my code?

Here is my current code

local canPick = 0
local part = script.Parent
local function onTouch(part)
game.Workspace.LogValue.Value = true
end
local function onTouchEnded(part)
game.Workspace.LogValue.Value = false
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

1 Like

make sure canCollide is turned on.

2 Likes

LocalScripts don’t run in ReplicatedStorage. You can easily test this by trying to print something.

2 Likes

I think collisions can only be detected if the part is a descendant of the Workspace.

Workspace is the only place where physics are applied to parts.

Scripts will only run in Workspace or ServerScriptService.

LocalScripts need to be either in the player’s PlayerScripts, a descendant of the player’s PlayerGui, or in their character model.

Put the script in starter GUI instead. Local scripts don’t run the the workspace. Corresponding, change the script to this:

local canPick = 0
local part = workspace.PartNameHere
local function onTouch(part)
    game.Workspace.LogValue.Value = true
end
local function onTouchEnded(part)
    game.Workspace.LogValue.Value = false
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
1 Like

It’s in ReplicatedStorage though, not in the Workspace.

@NotOriginal12345 I would recommend putting the script in StarterPlayerScripts.

@VegetationBush I would not recommend putting the script in StarterGUI because it is supposed to be for GUI Objects, hence the name has GUI in it.

1 Like

A part in ReplicatedStorage won’t even render, let alone register Touched events.

The thing in ReplicatedStorage get’s cloned into the workspace.

Where else would you put it? StarterGUI scripts not necessarily have to be related to UI.

Parts have to be in the workspace to be able to detect touches.

If it gets cloned into the workspace do this, make sure it’s a local script in startergui:

local ReplicatedStorage = game:GetService("ReplicatedService")
local canPick = 0
local part = ReplicatedStorage:WaitForChild("PartNameHere")
local Cloned = part:Clone() -- Make a variable for the touched part
local function onTouch(part)
    game.Workspace.LogValue.Value = true
end
local function onTouchEnded(part)
    game.Workspace.LogValue.Value = false
end
Cloned.Touched:Connect(onTouch)
Cloned.TouchEnded:Connect(onTouchEnded)

Is the LocalScript inside the part?

If so,

My suggestions to this are:

  • RemoteEvent that will Fire to the client if the Part is touched to toggle the Log Value.
  • Put the LocalScript in to places where it will run.

Put non-Gui scripts in StarterPlayerScripts or StarterCharacterScripts

1 Like