Note: First time asking for help on DevForum.
-
What do you want to achieve? Keep it simple and clear!
I am making a a power testing game, and I am trying to make a clock accessory attach to the player’s chest (it needs to function related to the TimeOfDay).
-
What is the issue? Include screenshots / videos if possible!
I have pretty good experience with scripting and accessories, but when I attached the clock, the script that moved the minute and hour hands stopped working properly (the script moves the CFrame of them to the right position every second cause of the TimeOfDay script I made).
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried to use body movers such as BodyPosition and BodyGyro. This almost worked out after I set it to update using while wait() do loop, but when I moved with it, it would fall behind the character trying to catch up. Video below (don’t worry about the gear on the face lol):
Is there a better way to make a part seemed anchored without actually anchoring it (CanCollide off and welds don’t work with the script)?
4 Likes
Just weld it to the character.
“welds don’t work with the script”
how are the hands attached? Try setting the model’s Network Owner to the player.
Put this ServerScript in both of the hands:
local plrs = game.Players
local hand = script.Parent
plrs.PlayerAdded:Connect(function(plr)
hand:SetNetworkOwner(plr)
end)
This should work.
It doesn’t work, I don’t think the problem is the Network Owner. Also the hand’s are attached atm through a BodyPosition and BodyGyro constantly being set.
1 Like
could you provide a script sample for the movement of the hands?
Here is part of the script that updates the BodyPosition, BodyGyro. (How do I do the block code or whatever?)
while wait() do
local hours, minutes = string.match(game.Lighting.TimeOfDay, “^(%d%d):(%d%d)”)
local hourHandCFRAME = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad((hours * 30)+ (minutes * 0.5))) * CFrame.new(0, 0.2, 0)
local hourHandVECTOR = hourHandCFRAME.p
local minuteHandCFRAME = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad(minutes * 6)) * CFrame.new(0, 0.3, 0)
local minuteHandVECTOR = minuteHandCFRAME.p
script.Parent.HourHand.BodyPosition.Position = hourHandVECTOR + Vector3.new(0,.2,0)
script.Parent.MinuteHand.BodyPosition.Position = minuteHandVECTOR + Vector3.new(0,.2,0)
local hourHandORIENTATION = hourHandCFRAME:ToOrientation()
local minuteHandORIENTATION = minuteHandCFRAME:ToOrientation()
script.Parent.HourHand.BodyGyro.MaxTorque = Vector3.new(hourHandORIENTATION)
script.Parent.MinuteHand.BodyGyro.MaxTorque = Vector3.new(minuteHandORIENTATION)
–
local hours, minutes = string.match(game.Lighting.TimeOfDay, “^(%d%d):(%d%d)”)
script.Parent.HourHand.CFrame = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad((hours * 30)+ (minutes * 0.5))) * CFrame.new(0, 0.2, 0)
script.Parent.MinuteHand.CFrame = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad(minutes * 6)) * CFrame.new(0, 0.3, 0)
end
do (```) at the beginning and end if the code. (Without the Brackets. Just the quotations.)
Here is the code:
while wait() do
local hours, minutes = string.match(game.Lighting.TimeOfDay, “^(%d%d):(%d%d)”)
local hourHandCFRAME = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) *
CFrame.Angles(0, 0, math.rad((hours * 30)+ (minutes * 0.5))) * CFrame.new(0, 0.2, 0)
local hourHandVECTOR = hourHandCFRAME.p
local minuteHandCFRAME = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) *
CFrame.Angles(0, 0, math.rad(minutes * 6)) * CFrame.new(0, 0.3, 0)
local minuteHandVECTOR = minuteHandCFRAME.p
---
script.Parent.HourHand.BodyPosition.Position = hourHandVECTOR + Vector3.new(0,.2,0)
script.Parent.MinuteHand.BodyPosition.Position = minuteHandVECTOR + Vector3.new(0,.2,0)
---
local hourHandORIENTATION = hourHandCFRAME:ToOrientation()
local minuteHandORIENTATION = minuteHandCFRAME:ToOrientation()
---
script.Parent.HourHand.BodyGyro.MaxTorque = Vector3.new(hourHandORIENTATION)
script.Parent.MinuteHand.BodyGyro.MaxTorque = Vector3.new(minuteHandORIENTATION)
---
local hours, minutes = string.match(game.Lighting.TimeOfDay, “^(%d%d):(%d%d)”)
---
script.Parent.HourHand.CFrame = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad((hours * 30)+ (minutes * 0.5))) * CFrame.new(0, 0.2, 0)
script.Parent.MinuteHand.CFrame = script.Parent.HandCenter.CFrame * CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, 0, math.rad(minutes * 6)) * CFrame.new(0, 0.3, 0)
end
While wait() do
: Try changing wait()
to true
. Keep in mind, make sure to save the game before you do this. this may cause your game to crash, but im not too sure.
1 Like
It will, I’ve had scripting experience before, if you don’t put a wait() it will crash it, is there anything faster than wait()?
1 Like
I think I found something faster, game:GetService("RunService").RenderStepped:connect
might work better, not sure if it’ll fix the problem entirely though.
1 Like
It seems it can only be used from a LocalScript though.
well, wait()
takes the rendertime from your own computer so it really depends.
To justify this, try using wait(.05)
.
Maybe using RenderStepped
would fix this. I am not too experienced with this but it may work.
So it appears that the clock is moved to the character while the game is running. In this case, put this LocalScript in StarterCharacterScripts:
local rs = game:GetService("RunService")
local Char = script.Parent
local Clock = Char.--Clock Name here
local handHour = Clock.--Hour Hand Name here
local handMinute = Clock.--Minute Hand Name here
rs.RenderStepped:Connect(function()
--try to negate the hand lag here.
--maybe use handhour.BodyPosition.Position = handhour.BodyPosition.Position + Vector3.new(2,0,0)
--or something like that.
end)
The only problem is it needs to be through a server script (everyone has to be able to see it not just the localPlayer). I could use RemoteEvents perhaps.
I tried it, and it doesn’t work still, the hands keep falling behind the character when I walk. It seems to be a bit closer though, is there a better way to do it than Body movers without using welds or anchoring…?
well if they seem closer, try lowering the wait(.05)
time to be even lower; maybe like wait(.001)
The main problem here is that you are using wait()
. Try to lower the time until it seems good. Don’t go too low thoguh. If you think you are going too low, than let me know and we can think of another solution.
Im not too sure tbh. You can try instead of setting just the Y position and rotation, you can set the Y and X (or Z) position and Y rotation.
you can add a loop inside of the existing loop:
while wait(.01) do
script.Parent.HourHand.CFrame = script.Parent.HourHand.CFrame + CFrame.new(2,0,0) --change "2" to fine tune it. You may need to move it to the Z coordinate instead.
end
Well, the thing is, renderStepped is supposed to be faster than any wait(), and I’m using that, and it’s still behind. Also keep in mind it works perfectly fine when I stand still.
I think the best thing to do would be to find another way to keep the hands on the clock rather than body movers.
yeah I think you might be right about that. BodyMovers should not be moved themselves (ie. the character moving) but instead cause the movement.
Maybe use the clock model for something else, like a Clock Tower. You don’t want that to go to waste
It looks pretty cool.
The other thing I can think are WeldConstraints and Welds. Make sure you set the NetworkOwner of the model’s PrimaryPart to be the LocalPlayer if you are going to use welds.
I can’t use welds, when I connect the welds to the clock, it’ll stick there and won’t be able to move (it’ll legit like turn gravity XD)
I don’t want to disappoint my friend, there has to be some way to do it properly.