Script only works in studio

This script only works in studio, it lets you drag parts around with your mouse. How do I fix? (It’s a localscript in starterplayerscripts)

local mouse = player:GetMouse() 
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService") --we will use this service for something later
local target 
local down

function mouseHit(distance) 
	local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * distance) --as you can see, here creating a ray with the same origin (which is the camera of course) and the same direction BUT longer, whatever the distance parameter is
	local _, position = workspace:FindPartOnRay(ray)

	return position 
end

if mouse.Target ~= nil then
mouse.Target.Position = mouseHit(20)
mouse.Target.Position = camera.CFrame.Position + (mouse.Hit.Position.Unit * 20)
end


mouse.Button1Down:connect(function() 
	if mouse.Target ~= nil and mouse.Target.Locked == false then
		target = mouse.Target 
		mouse.TargetFilter = target 
		down = true 

		local gyro = Instance.new("BodyGyro") --adding the forces
		gyro.Name = "Gyro"
		gyro.Parent = target 
		gyro.MaxTorque = Vector3.new(500000, 500000, 500000)
		local force = Instance.new("BodyPosition") 
		force.Name = "Force" 
		force.Parent = target
		force.MaxForce = Vector3.new(10000, 10000, 10000) --you may wanna modify this a bit, since it effect if you can move an object or wrong depending on its weight/mass (in other words, the force might not be strong enough)
	end
end)

game:GetService("RunService").RenderStepped:Connect(function() --replaced the move event with renderstepped because it works better in some cases, renderstepped is an event that fires every frame, basically super fast, look it up it is important!
	if down == true and target ~= nil then 
		target.Gyro.CFrame = target.CFrame --this is to keep rotation
		target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 20)
	end 
end) 

mouse.Button1Up:connect(function() 
	if target then --of course we wanna remove the forces after the dragging, first check if there was even a target
		if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then --check if there was a force
			target.Gyro:Destroy() --DESTROY!!!!
			target.Force:Destroy()
		end
	end
	down = false
	mouse.TargetFilter = nil
	target = nil 
end)```
1 Like

Did you not paste the whole script? For your first line it is referencing a variable named “player” but I do not see that defined anywhere.

Do you mean local player = game.Players.LocalPlayer?
im putting the script together from this: Dragging objects with the mouse

Yes. That needs to be at the top of your script.

it is at the top of the script though. :confused:

Apologies, it definitely wasn’t at the top of the code you pasted to show us. Try putting a wait(5) above your entire script and see if that works. Also, are you getting any errors?

No errors, but the code works in studio testing, but not in actual game.

doing the wait didn’t work. Another weird thing was that everything except the spawn plate seems to move down when the script is enabled.