hi, can i calculate how many studs an object has fallen, and if the studs are more than a certain amount then something happens? this is probably about magnitude, i’m not just that confident with it, i need to learn more. thank you ^^
You could get the starting Y position of the part then run a heartbeat function to constantly check if the starting Y is farther than a certain amount of studs away by subtracting them like
startingY - currentY
And then disconnect the function after you’ve done what you needed to do
wait, i don’t think i fully understood what i need to do
So you’d get the part’s starting Y position when the script first runs by making it a variable
local startingY = script.Parent.Position.Y
Then after you’d want to make the amount of studs it’s fallen a variable like at which point it’s going to do something
local stoppingPoint = 50
Then the function so you want to use a heartbeat connection because it runs basically constantly so first you’d make the connection a variable so we can disconnect it later
local connection
Then the function we’re gonna use. I made a simple checking function which just subtracts the variables and checks if it’s greater than or equal to the stopping point
function checkPos()
local currentY = script.Parent.Position.Y
local distance = startingY - currentY -- Getting the current distance
if distance >= stoppingPoint then -- Checking if it's greater than or equal to
-- Run code
connection:Disconnect() -- Disconnecting
end
end
Lastly, we make make the connection variable from earlier equal the heartbeat connection
connection = heartbeat:Connect(checkPos)
All of the code in one place:
local RunService = game:GetService("RunService")
local heartbeat = RunService.Heartbeat
local startingY = script.Parent.Position.Y
local stoppingPoint = 50
local connection
function checkPos()
local currentY = script.Parent.Position.Y
local distance = startingY - currentY -- Getting the current distance
if distance >= stoppingPoint then -- Checking if it's greater than or equal to
-- Run code
connection:Disconnect() -- Disconnecting
end
end
connection = heartbeat:Connect(checkPos)
Hopefully this helped clear some stuff up I’m not the best at explaining things LOL
thanks, it works, but if i insert this object inside the model of a player (the player character) it doesn’t work anymore :(. any idea on how to fix it?
How are you inserting it ? Like from a script ? If so where is the part originally located like is it in ServerStorage or somewhere else
just in the custom startercharacter, placed in StarterPlayer
Wait so the script is inside StarterPlayer or the part is ?
Oh nevermind I get what you’re saying
yeah, let me know if i wasn’t clear, if you know how to fix it please let me know ^^
So I just found out that all scripts that you’ve put into the StarterCharacter model don’t get copied over with the model so what you’ll need to do is put the script inside StarterCharacterScripts and edit it a little. Here’s the script I used if you want it
local RunService = game:GetService("RunService")
local heartbeat = RunService.Heartbeat
local StarterPlayer = game:GetService("StarterPlayer")
repeat heartbeat:Wait() until script.Parent ~= StarterPlayer.StarterCharacterScripts
local startingY = script.Parent.Part.Position.Y -- Part is the part inside your Character
local stoppingPoint = 5
local connection
function checkPos()
local currentY = script.Parent.Part.Position.Y
local distance = startingY - currentY -- Getting the current distance
if distance >= stoppingPoint then -- Checking if it's greater than or equal to
-- Run code
connection:Disconnect() -- Disconnecting
end
end
connection = heartbeat:Connect(checkPos)
thank you, you were very kind and patient
No problem and good luck!
Hi, sorry to bother you again. The script works really well, I was just wondering if there is any way to act on the part after it touched the ground and not while it’s in the air. For example, the stoppingpoint
is 5 and the part falls from 10 studs, the current code will act on the part when it will hit the 5th stud. So my question is: is there any way to act on the part only once it landed? Thank you
This script just checks when it’s velocity is at 0 so when it’s not moving at all. Which in different situations this wouldn’t really be the best way to do it but hopefully for yours it’ll work. Here’s the code:
local RunService = game:GetService("RunService")
local heartbeat = RunService.Heartbeat
local StarterPlayer = game:GetService("StarterPlayer")
repeat heartbeat:Wait() until script.Parent ~= StarterPlayer.StarterCharacterScripts
local part = script.Parent.Part
local connection
function checkPos()
if part.AssemblyLinearVelocity == Vector3.new() then
-- Run code
connection:Disconnect() -- Disconnecting
end
end
connection = heartbeat:Connect(checkPos)
This script should also be in StarterCharacterScripts btw if it’s not then you’ll have to edit it a little
where does it check if the part has gone further than it should have? perhaps i wasn’t clear enough? i’ll try to explain it better. in the code you gave to me, if the stopping point is 5 for example, the code will act in the moment the part goes further than the 5th stud. but if it’s falling from an altitude of 25 studs, the code will act when there are still 20 studs to go through. i want the code to act when the part hits the ground, not while it’s still falling. was this clear? let me know if not!
This script should work:
local RunService = game:GetService("RunService")
local heartbeat = RunService.Heartbeat
local StarterPlayer = game:GetService("StarterPlayer")
repeat heartbeat:Wait() until script.Parent ~= StarterPlayer.StarterCharacterScripts
local part = script.Parent.Part -- Part is the part inside your Character
local startingY = part.Position.Y
local connection
function checkPos()
local currentY = part.Position.Y
local distance = startingY - currentY -- Getting the current distance
if distance >= startingY - part.Size.Y then -- Checking if it's greater than or equal to
-- Run code
connection:Disconnect() -- Disconnecting
end
end
connection = heartbeat:Connect(checkPos)
It basically just takes the starting Y position and subtracts it from the part’s Y size and checks if it’s further than that
it works well until this line
the size comparation messes up the code, i tried changing startingY - part.Size.Y
to the number of the studs and it works well, but it has the same result of the first code you gave me
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.