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

Magnitude showing up as 0?

Asked by 5 years ago

I was playing around with this script and hoped to make a boss battle script out of it somehow, and I'm doing decent so far. Basically, I have a textbutton teleporting you to the boss in an arena, and I was hoping that if the player was in a certain range of the boss, the boss would attack and music would play. The problem is that the music starts playing as soon as I spawn, even though the boss is extremely far away. I added a wait to the PlayerAdded function, and the music played when I was in the arena, but it also played when I was extremely far away from the boss as well. I decided to print the magnitude of the boss' torso position - the player's torso position, and it always prints 0, no matter where I am.

The script:

function ShootingStar(plrtorso,plr)

    while true do


        local Star = game.Workspace.Fire:Clone()
    Star.Name = "SunComet"
    Star.Anchored = false
    Star.CanCollide = false
    Star.Parent = workspace.MagicParts
    Star.CFrame = script.Parent.Torso.CFrame +Vector3.new(0,0,-6)
    Star.SunExplosion.Enabled = false

    local bv = Instance.new("BodyVelocity")
    bv.Parent = Star
    bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    bv.P = 800


    bv.Velocity = (plrtorso.Position - script.Parent.Torso.Position).unit*50





    Star.Touched:Connect(function(hit)

        Star:Destroy()

        local Sun = Instance.new("Part")
        Sun.Parent = workspace.MagicParts
        Sun.Material = "Neon"
        Sun.BrickColor = BrickColor.new("Deep orange")
        Sun.Position = Star.Position
        Sun.Shape = "Ball"
        Sun.Size = Vector3.new(0,0,0)
        Sun.Transparency = 0
        Sun.CanCollide = false
        Sun.Anchored = true

        local SunPE = game.Workspace.Fire.SunExplosion:Clone()
        SunPE.Parent = Sun
        SunPE.Enabled = true

        for i = 1, 30 do

            Sun.Size = Sun.Size + Vector3.new(2,2,2)
            Sun.Transparency = Sun.Transparency + 0.05


            wait()
        end

        local h = hit.Parent:FindFirstChild("Humanoid")
        local pv = hit.Parent:FindFirstChild("PlayerValue")

        if h ~= nil then

            if pv ~= nil then


                if pv.Value == plr.Name then

                    h:TakeDamage(0)


                end



            end

            if h~=nil then


                if pv == nil or pv.Value ~= plr.Name then

                    h:TakeDamage(3)

                end

            end

        end


    game.Debris:AddItem(Sun,5)


    end)                    

    game.Debris:AddItem(Star,10)    














        wait(40)




    end



end



game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.Character
    local char = plr.Character

    wait(10)


    local torso = char.Torso

    local theos = script.Parent

    local PV = Instance.new("StringValue")
    PV.Name = "PlayerValue"
    PV.Parent = plr.Character
    PV.Value = plr.Name

    dist =50

    local body = game.Workspace:GetChildren()
    for x = 1,#body do
        local temp = body[x]
        if temp.ClassName == "Model" and temp ~= plr.Character then
            local hum = temp:FindFirstChild("Humanoid")
            bod = temp:FindFirstChild("HumanoidRootPart")
            if hum ~= nil and hum.Health > 0 then
                if bod then
                    if (theos.HumanoidRootPart.Position - bod.Position).magnitude < dist then
                        person = temp

                        if person ~= nil then
                            local b = (bod.Position - theos.HumanoidRootPart.Position).magnitude
                            print(b)


                            local Sound = Instance.new("Sound")
                            Sound.Parent = char.Torso
                            Sound.SoundId = "rbxassetid://622902771"
                            Sound.MaxDistance = 300
                            Sound:Play()

                            ShootingStar(torso,plr)

                        end

                    end
                end

            end         
        end
    end
end)
1

1 answer

Log in to vote
1
Answered by 5 years ago

This loops over all children of the Workspace:

local body = game.Workspace:GetChildren()

This includes Models, but excludes your player's Character Model:

if temp.ClassName == "Model" and temp ~= plr.Character then

So my guess is that, most of the time if it's just you testing, this check:

local b = (bod.Position - theos.HumanoidRootPart.Position).magnitude

Is comparing theos's position to theos's position, and he's zero studs away from himself.

Ad

Answer this question