Local script not working after enabling it

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

  2. 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.

  3. 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.

Just looked it up and I was right, so here ya go

1 Like

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

So if a I have a folder that contains all the cubes, to make the script work I need to loop through all cubes and check if mouse.Target == cube?

yeah, that’ll work!

-- ...
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
-- ...
1 Like

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