No reason why code doesn't run right away

I recently read through a dev forum tutorial about making an object dragging system similar to Lumber Tycoon 2’s. The post is here if you want to read it. The tutorial was solid and I learned a lot but I ran into an issue that I’m not sure if anyone else experiences. For some reason when I try and drag parts in my game, it doesn’t move for the first 10-20 seconds. After I wait a bit the parts finally start to move when I try and drag them and I have no idea what is causing this issue. If someone could explain this for me that would be great. Also, I ran this in a blank game so I’ve come to the conclusion that it isn’t lag from an abundance of scripts running that is causing this problem.

local player = game.Players.LocalPlayer 
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



mouse.Button1Down:connect(function() 
	if mouse.Target ~= nil and mouse.Target.Locked == false and mouse.Target == workspace.Pizza.Part 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)
	else
		print("No Pizza To Move")
	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)

Your issue probably relates to network ownership. Since you are creating the bodyposition/bodygyro locally and modifying it locally, you need to “own” the part for physics simulations to occur properly.

To test this, in a server script, try running game.Workspace.Pizza.Part:SetNetworkOwner(game.Players["yournamehere"]) – making sure that your player has loaded in before this line runs.

Also, make sure that your part is not anchored and not welded to an anchored part.

The reason why your script might not work for the first 10-20 seconds is probably because at the beginning, the part is “owned” by the server. Eventually, if you are near that part, the server may automatically assign you as the “owner” of the part, and your client will be responsible for physically simulating it (read the article linked above for more information), which is why after 10-20 seconds you can move the part.

2 Likes

Thanks for the explanation for what is going wrong. I typed this up and I’m not sure if I did something wrong but it doesn’t work. Do you mind checking it out?

function setOwner()

game.Workspace.Pizza.Part:SetNetworkOwner(game.Players["TooMuchRice"])

end

local found = game.Players:WaitForChild("TooMuchRice")

if found ~= nil then

setOwner()

print("Ran")

end

Some questions:

  • Is this a server script?

  • Are there any warnings/errors in the output?

  • Is “Ran” printed?

Yeah it is a server script and there no errors and Ran is never printed.

Make sure that your server script is not disabled and that it’s running in a valid location (e.g. ServerScriptService).

Also, make sure that your player name is correct (running in a test server could result in your name being ‘Player1’). Try this approach instead:

game:GetService("Players").PlayerAdded:Connect(function(Player)
	print(Player.Name .. " joined.")
	game.Workspace.Pizza.Part:SetNetworkOwner(Player)
end)

Assuming you’re running this in play solo, you should become the owner of the part, and the local script should work.

It prints my name which is good but there is still a slight delay. I guess there is nothing I can really do about it but thanks.