I know this is a stupid question, but I’ve been thinking about it and searching the forum for a long time, but I can’t find an answer. So, I need to have a script to detect the magnitude of a players’ humanoidrootpart to a part, and when it comes close to it, it will shine, how will I do that?
So what you’d need to do is constantly check the magnitude, and see if it’s less than the check range.
while wait() do
if (humrootpart.Position-part.Position).magnitude < checkRange then
--Shine here
end
end
Hope this helps!
No real need for magnitude. The engine has a built-in method for detecting the distance between a character and a given position, Player.DistanceFromCharacter If you use this function and Roblox changes its rig construct in the future, the internal calculation would get changed and you wouldn’t have to worry about your code breaking. Fun fact: HumanoidRootPart didn’t always exist.
This function checks the distance between the head and a given point though, so it might not completely satisfy your needs. If you need explicit checking between the HumanoidRootPart and a given position, what you’re aiming to do is do a vector subtraction between both points then get the absolute distance of the resultant vector.
The code in the post above should suit your needs, though just one thing - it runs into a bad pattern with while loops that uses wait as a condition. I shared a document regarding this some time ago, The While-Wait-Do Idiom. The proper way to write that (cc @Protori) would be:
while true do
if (Root.Position - Part.Position).Magnitude < checkRange then
-- Shine
else
-- No shine
end
wait()
end
Note: The vector subtraction is not used as the condition, otherwise it’d be a terminating loop which is not the intended goal of a system like this. The proper way to access Magnitude currently is also through its UpperCase property.
There is one thing I want to take note, if you go and use the method of :DistanceFromCharacter(), it has an edge case and you would want to check to see if the humanoid is actually alive. Because it returns 0 if the Character has died.
Aside: Why does Player.DistanceFromCharacter
even exist? It’s a rather specific case that implements simple operations.
So, would I use a local script or a server script? And one extra question, how can I tween the size of a part. Let’s say, inout sine.
Use a LocalScript if only they should see the shine, otherwise use a Script to replicate it to all clients.
You can tween a part using TweenService, and determine the EasingDirection/EasingStyle in the TweenInfo. I’d specify the TweenInfo as a variable to decrease the line length.
local ts = game:GetService("TweenInfo")
local ti = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local part = workspace.Part
local tween = ts:Create(part, ti, {Size = Vector3.new(5,5,5)})
tween:Play() --tweens it's size to 5,5,5
Also, remember that with GuiObject:TweenPosition()
/GuiObject:TweenSize()
the EasingDirection comes first and then the EasingStyle comes next whereas in TweenInfo the EasingStyle comes first and the EasingDirection comes second.
For magnitude, there are many ways to format it but the most common and easiest to use would be;
( HumanoidRootPart.Position - Part.Position).magnitude
Now, say you wanted to check if a player was in distance of the part, you could do as so:
local isInDistance = (HumanoidRootPart.Position - Part.Position).magnitude if isInDistance <= 10 then -- this is the max distance you want
I tried doing it but it doesn’t work:
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local part = script.Parent
game.Players.PlayerAdded:Connect(function(player)
local HRP = player.Character:WaitForChild("HumanoidRootPart")
while true do
if (HRP.Position - script.Parent.Position).Magnitude <= 15 then
local tween = ts:Create(part, ti, {Size = Vector3.new(15,15,15)})
tween:Play()
else
local tween = ts:Create(part, ti, {Size = Vector3.new(0.88,0.88,0.88)})
tween:Play()
end
wait()
end
end)
it’s in a server script
It’s helpful because there might not be a Character available (e.g. right when the player enters the game). However, I would argue they implemented it badly: If there’s no player, it returns 0. IMO it should return something like -1.
I’d imagine some of the internal methods are also hooked into c++ right? Which would mean much better performance in most cases. I could be wrong and the player class could be lua based I’m not sure
You’re possibly indexing player.Character before the Character exists, try using the CharacterAppearanceLoaded event:
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local part = script.Parent
local increaseTween = ts:Create(part, ti, {Size = Vector3.new(15,15,15)})
local decreaseTween = ts:Create(part, ti, {Size = Vector3.new(0.88,0.88,0.88)})
--Create the tweens only once
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
while true do
local HRP = character:WaitForChild("HumanoidRootPart")
if (HRP.Position - script.Parent.Position).Magnitude <= 15 then
increaseTween:Play()
else
decreaseTween:Play()
end
wait()
end
end)
end)
Is it possible to tween colours?
Is it possible to tween the position of an object?
From the TweenService documentation:
Both colors (Color3) and object positions (Vector3) are tweenable.
How would I do that? I just need to tween the y position
Changing one axis in a Vector3 requires you to fill in the other axis with the current values.
Here’s an example:
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(2)
local part = workspace.Part
local tween = ts:Create(part, ti, {Position = Vector3.new(part.Position.X, 5, part.Position.Z) })
tween:Play() --tweens to 5y without changing the X nor Z
Sorry about this, I have one last question, is there a different between using () [] and {}? And to tween rotation you use rotation?
Can you elaborate upon which context?
For GuiObjects use Rotation (number) whereas for BaseParts use Orientation (Vector3) or CFrame (CFrame).
like is using the different parameters gonna have a difference?