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

How do I convert the original position to studs?

Asked by 2 years ago
Edited 2 years ago

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)

0
@zakherdabber i have fixed the script i made, read the reason below my post! Antelear 185 — 2y
0
@zakherdabber i have fixed the script i made, read the reason below my post! Antelear 185 — 2y
0
Thank you very much zakherdabber 7 — 2y
0
"ZakherDabber died at: 7,4,-6 - Server - Script:27" zakherdabber 7 — 2y
View all comments (2 more)
0
Works Well! zakherdabber 7 — 2y
0
I'm glad it does! Antelear 185 — 2y

1 answer

Log in to vote
0
Answered by
Antelear 185
2 years ago
Edited 2 years ago

INTRODUCTION

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):

EXPLANATION

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!

THIS IS A SERVER SCRIPT!

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)

FURTHER EXPLANATION

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.

Conclusion

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).

0
(NOTE: I'VE DONE THIS BASED OFF OF PURE MEMORY, IF I MADE A MISTAKE THEN EXPECT IT, I WILL FIX ANY ERRORS THAT YOU CANNOT FIX BY YOURSELF SO PLEASE DON'T BE AFRAID TO ASK!) Antelear 185 — 2y
0
Amazing! Now I at least know a new type of function which is "math.floor". This answer is well-appreciated zakherdabber 7 — 2y
0
ServerScriptService.Script:33: invalid argument #2 to 'remove' (number expected, got string) and ServerScriptService.Script:13: invalid argument #1 to 'find' (table expected, got nil) zakherdabber 7 — 2y
0
Oh...Sorry man, I told you I've done it based off of pure memory. Antelear 185 — 2y
Ad

Answer this question