So basically I made a code where it detects the position of the player where it died on but I want the code to tell me the position in studs.
game:GetService('Players').PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) character:WaitForChild("Humanoid").Died:Connect(function() local humanoidRootPart = character:WaitForChild("HumanoidRootPart") print(player.Name, "died at", tostring(humanoidRootPart.Position)) wait(3) end) end) end)
You should use RunService
. RunService
is a Service on Roblox that is like while loops but more optimised. There's two you can use in this situation (depending on the script):
RS.Heartbeat - Description: The Heartbeat event fires every frame, after the physics simulation has completed.
RS.RenderStepped - Description: The RenderStepped event fires every frame, prior to the frame being rendered.
Heartbeat
is strictly for server scripts and RenderStepped
is strictly for local scripts, Keep this in mind. Now let's think of a solution...
I don't know what you meant by studs, but I assume you meant in whole numbers so I used math.floor
, which is a math function which turns any decimal numbers into a whole number. So for example if you had 1.01
it would then become 1
. Now let's get to scripting!
local Users = game:GetService("Players") local RS = game:GetService("RunService") UserPositions = {} Users.PlayerAdded:Connect(function(User) local HRPPos local PosX local PosY local PosZ -- Add them outside the scope so it doesn't error later on. if table.find(UserPositions, User.UserId) then return end local Char = User.Character or User.CharacterAdded:Wait() RS.Heartbeat:Connect(function() HRPPos = Char:WaitForChild("HumanoidRootPart").Position PosX = math.floor(HRPPos.X) PosY = math.floor(HRPPos.Y) PosZ = math.floor(HRPPos.Z) end) -- this will constantly update their position! task.wait(0.1) UserPositions[User.UserId] = PosX..","..PosY..","..PosZ Char.Humanoid.Died:Connect(function() print(User.Name.." died at: "..UserPositions[User.UserId]) end) end) Users.PlayerRemoving:Connect(function(User) table.remove(UserPositions, User.UserId) end)
Now at first this looks confusing for most and I get that, but let me explain!
UserPositions
is a table that adds any player's name into it. Though since no players have joined quite yet it is empty. But let's say "coolman153" joins your game, the PlayerAdded
function will go through and check if he's already in the table. If he is it will stop the script here, just in case.
But if he isn't in the table then we will continue with the script! We make the variable Char
checking User.Character
or :Wait()
for the Character to be added via CharacterAdded
. Now that we have done this we will make thw HRPPos
(can be any other variation) and use our Char
variable to find HumanoidRootPart
to then find its Position
. Then we make Pos
X through Z and then like I said before use math.floor
to make the numbers WHOLE, and assign the variable to the positions x-z.
Now we do UserPositions[User.Name] = PosX..", "..PosY..", "..PosZ
. We made a STRING, which in short is text. We use ..
to concatenate the 3 variables together with the added spaces! The reason I use the two brackets ([]) is because this will either add or change a piece of the table depending on if User.Name
exists in it or not. Then when we're done it will add them into the table!
The PlayerRemoving
function is to remove the User from UserPositions
, as we wouldn't need them again since they left the game. This is to make sure you don't have like 100k players in one table as it could take a small bit to print right after.
Hey I hope this helped you understand the problem and hopefully you learned from this as well! If it did make sure to mark this answer as the answer that helped you! :D
(I CHANGED THE SCRIPT, IT SHOULD 100% WORK NOW! (the table expects a number not a string, so instead you use the player's userid).