What do you want to achieve?
I want to enable my cube drag local script whenever a new cube spawns (the cube is being cloned from replicated storage and parented to workspace.cubes folder).
What is the issue?
The issue is that my script just doesn’t work at all when I try enabling it, even though the script is parented to the cube. There are no errors in the output.
What solutions have you tried so far?
I tried to fix it myself, but I came up with nothing. I looked for the solution on the devforum too.
Here is the code of the dragging script:
local module = require(game.StarterPlayer.StarterPlayerScripts.modules.modScriptAct.grabCube)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local cube = script.Parent
local function onMouseDown()
print(mouse.Target)
if mouse.Target == cube then
module.Grab(cube)
end
end
local function onMouseUp()
print(mouse.Target)
if mouse.Target == cube then
module.StopGrabbing(cube)
end
end
mouse.Button1Down:Connect(onMouseDown)
mouse.Button1Up:Connect(onMouseDown)
By default, LocalScripts will not run under workspace (unless in the character) , to resolve this problem you can either: Move the LocalScript into StarterPlayerScripts. Convert the LocalScript into a Script and change the RunContext to Client.
like @FroDev1002 said, you can’t parent it under the cube in the workspace or it wouldn’t run.
You’ll have to parent the script in something like StarterGui or PlayerScripts (if I remember correctly) and the reference the cube in the workspace
local module = require(game.StarterPlayer.StarterPlayerScripts.modules.modScriptAct.grabCube)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local cube = game.Workspace.cube -- PATH TO CUBE?
local function onMouseDown()
print(mouse.Target)
if mouse.Target == cube then
module.Grab(cube)
end
end
local function onMouseUp()
print(mouse.Target)
if mouse.Target == cube then
module.StopGrabbing(cube)
end
end
mouse.Button1Down:Connect(onMouseDown)
mouse.Button1Up:Connect(onMouseDown)
-- ...
local folderOfCubes = --PATH
local grabbedCube = nil -- Reserved
local function onMouseDown()
print(mouse.Target)
for _, cube in ipairs(folderOfCubes:GetChildren()) do
if mouse.Target == cube then
module.Grab(cube)
grabbedCube = cube
end
end
end
local function onMouseUp()
module.StopGrabbing(grabbedCube)
end
-- ...