Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need help with Heartbeat and stuff?

Ok, so I am moving a crystal above a players head. And the problem is that it follows but doesn't keep up. It is super jittery and jumpy. FE is enabled

SCRIPT IN WORKSPACE: (It has a Remote Event in the script.

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local part = game.ReplicatedStorage.Crystal
		local clone = part:Clone()
		clone.Parent = workspace
		clone.Name = "PlayerCrystal"
		script.RemoteEvent.OnServerEvent:connect(function()
			local head = character["Head"]
			local objectSpace = CFrame.new(0, 2, 0)
			local worldSpace = head.CFrame:toWorldSpace(objectSpace)
			clone.CFrame = worldSpace
		end)
	end)
end)

LOCAL SCRIPT IN STARTERGUI:

game:GetService("RunService").Heartbeat:connect(function()
	workspace.CrystalMove.RemoteEvent:FireServer()
end)

-Please, I really need to know how to fix this. (I am also looking for a scripter, Msg me on Roblox if you are interested, my username is KuroNakamura)

2 comments

0

Is the part itself anchored?

0

Why don't you just put it in the same script?

1 answer

This usage of Heartbeat circumvents the entire point of using it in the first place. The latency from the RemoteEvent makes the gains null.

You can also just use Heartbeat on the Server anyway:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local part = game.ReplicatedStorage.Crystal
        local clone = part:Clone()
        clone.Parent = workspace
        clone.Name = "PlayerCrystal"
       game:GetService("RunService").Heartbeat:connect(function()
            local head = character["Head"]
            local objectSpace = CFrame.new(0, 2, 0)
            local worldSpace = head.CFrame:toWorldSpace(objectSpace)
            clone.CFrame = worldSpace
        end)
    end)
end)

This code has a problem, however: it doesn't disconnect the event when that Character dies. I'll leave it as an exercise to you to figure out how to do that. Tip: the connect method returns something.


This will still be fairly jittery. The only way to remove the jitter is to use RenderStepped on the Client, and move the crystal there. Obvious this will only work for the specific Player and not everyone, so you can use both the above code and update the Client's crystal using RenderStepped, or update every Player's crystal using RenderStepped on the Client.

This discussion is preserved as it was. New answers, comments, and votes are closed.