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

(MATH) How to scale a player proportionally depending on the size of block they touch?

Asked by 4 years ago
Edited 4 years ago

I have a script that detects when a player touches a specific block and when they do, the player gets scaled according to how big the block was. However, I don't think I did the math correctly because after a while the player gains less and less even if they touched the same size block multiple times.

What currently happens is that the game calculates the volume of all of the player's body parts and adds them together to get the total volume of the player.

Then, it calculates the volume of the block the player touched.

Finally is does this formula: (((playerVolume + objectVolume) / playerVolume)) ^ (1/3)) - 1

(My reasoning for this formula is that because the player is being scaled proportionally, I can imagine moving the player's body parts around to form a cube (cubes sides are all the same) and so if I add the player's current volume to the object they touched and then divide by the player's current volume, it gives me the ratio between the player's volume before and after the object's volume is added to the player. Then, if I take the cube root of this ratio, I should get the scale values I need because I'm taking the cube root of the volume to get a one-dimensional value (height, width, and depth separately are one dimensional.) The minus one on the end is to get just the decimal.)

Theoretically, I thought this would work but it doesn't and I can't find anything online to help me.

It starts off really accurate but the larger the player gets, the less they receive by touching the blocks and this shouldn't happen but I don't know how to fix it.

It's not the the script doesn't work, it's that it gives me the wrong values over time.

The full script: (ugly but idk how else to do it)

local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("touchedEvent")

remote.OnServerEvent:Connect(function(player, part)

    --get player model
    local character = player.Character
    local humanoid = character:WaitForChild("Humanoid")

    --get scale values
    local headScale = humanoid:WaitForChild("HeadScale")
    local dScale = humanoid:WaitForChild("BodyDepthScale")
    local wScale = humanoid:WaitForChild("BodyWidthScale")
    local hScale = humanoid:WaitForChild("BodyHeightScale")

    -- get player parts lol
    local cHead = character:WaitForChild("Head")
    local cUTorso = character:WaitForChild("UpperTorso")
    local cRULeg = character:WaitForChild("RightUpperLeg")
    local cRUArm = character:WaitForChild("RightUpperArm")
    local cRLLeg = character:WaitForChild("RightLowerLeg")
    local cRLArm = character:WaitForChild("RightLowerArm")
    local cRHand = character:WaitForChild("RightHand")
    local cRFoot = character:WaitForChild("RightFoot")
    local cLTorso = character:WaitForChild("LowerTorso")
    local cLULeg = character:WaitForChild("LeftUpperLeg")
    local cLUArm = character:WaitForChild("LeftUpperArm")
    local cLLLeg = character:WaitForChild("LeftLowerLeg")
    local cLLArm = character:WaitForChild("LeftLowerArm")
    local cLHand = character:WaitForChild("LeftHand")
    local cLFoot = character:WaitForChild("LeftFoot")

    --begin awful math part

    --define part i touched
    local touched = part

    --define object volume
    local objectVolume = (touched.Size.X * touched.Size.Y * touched.Size.Z)
    print ("Object volume is "..objectVolume)

    --define current player volume
    local beforePlayerVolume = (cHead.Size.X * cHead.Size.Y * cHead.Size.Z) + (cUTorso.Size.X * cUTorso.Size.Y * cUTorso.Size.Z) + (cRULeg.Size.X * cRULeg.Size.Y * cRULeg.Size.Z) + (cRUArm.Size.X * cRUArm.Size.Y * cRUArm.Size.Z) + (cRLLeg.Size.X * cRLLeg.Size.Y * cRLLeg.Size.Z) + (cRLArm.Size.X * cRLArm.Size.Y * cRLArm.Size.Z) + (cRHand.Size.X * cRHand.Size.Y * cRHand.Size.Z) + (cRFoot.Size.X * cRFoot.Size.Y * cRFoot.Size.Z) + (cLTorso.Size.X * cLTorso.Size.Y * cLTorso.Size.Z) + (cLULeg.Size.X * cLULeg.Size.Y * cLULeg.Size.Z) + (cLUArm.Size.X * cLUArm.Size.Y * cLUArm.Size.Z) + (cLLLeg.Size.X * cLLLeg.Size.Y * cLLLeg.Size.Z) + (cLLArm.Size.X * cLLArm.Size.Y * cLLArm.Size.Z) + (cLHand.Size.X * cLHand.Size.Y * cLHand.Size.Z) + (cLFoot.Size.X * cLFoot.Size.Y * cLFoot.Size.Z)
    print("Player volume is "..beforePlayerVolume)

    --define the value to be added to the player scales
    local scaleAdd = (((objectVolume + beforePlayerVolume) / beforePlayerVolume)^(1/3)) - 1
    print("Scale value to add is "..scaleAdd)

    --update scale values
    headScale.Value = headScale.Value + scaleAdd
    dScale.Value = dScale.Value + scaleAdd
    wScale.Value = wScale.Value + scaleAdd
    hScale.Value = hScale.Value + scaleAdd

    --recalculate player volume
    local afterPlayerVolume = (cHead.Size.X * cHead.Size.Y * cHead.Size.Z) + (cUTorso.Size.X * cUTorso.Size.Y * cUTorso.Size.Z) + (cRULeg.Size.X * cRULeg.Size.Y * cRULeg.Size.Z) + (cRUArm.Size.X * cRUArm.Size.Y * cRUArm.Size.Z) + (cRLLeg.Size.X * cRLLeg.Size.Y * cRLLeg.Size.Z) + (cRLArm.Size.X * cRLArm.Size.Y * cRLArm.Size.Z) + (cRHand.Size.X * cRHand.Size.Y * cRHand.Size.Z) + (cRFoot.Size.X * cRFoot.Size.Y * cRFoot.Size.Z) + (cLTorso.Size.X * cLTorso.Size.Y * cLTorso.Size.Z) + (cLULeg.Size.X * cLULeg.Size.Y * cLULeg.Size.Z) + (cLUArm.Size.X * cLUArm.Size.Y * cLUArm.Size.Z) + (cLLLeg.Size.X * cLLLeg.Size.Y * cLLLeg.Size.Z) + (cLLArm.Size.X * cLLArm.Size.Y * cLLArm.Size.Z) + (cLHand.Size.X * cLHand.Size.Y * cLHand.Size.Z) + (cLFoot.Size.X * cLFoot.Size.Y * cLFoot.Size.Z)
    print("Player volume now is "..afterPlayerVolume)

    --get player's stats
    local leaderstats = player:WaitForChild("leaderstats")
    local volume = leaderstats:WaitForChild("Volume")

    --update leaderboard
    volume.Value = afterPlayerVolume

end)

Answer this question