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)
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)
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)