Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need help with Heartbeat and stuff?

Asked by 8 years ago

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)

0
Is the part itself anchored? GoldenPhysics 474 — 8y
0
Why don't you just put it in the same script? Overscores 381 — 8y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

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.

Ad

Answer this question