Mohannad El-Nesr
Mohannad El-Nesr

Reputation: 13

Roblox infinite rotating loop

I am working on doing a health pack for Roblox for my game. the code is complete and it works perfectly, but I want the health pack itself to rotate slowly in a cool way so here is my code tell me what is wrong

local healthPack = script.Parent
local healAmount = 30
local cooldown = 5
local canHeal = true

local function handleTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChild('Humanoid')
    if humanoid and canHeal then
        if game.Workspace.Player1.Humanoid.Health == 100 then

            print("You have enough health")

        else

        canHeal = false

        game.Workspace.HealthPack.Transparency = .75

            local currentHealth = humanoid.Health
            local newHealth = currentHealth + healAmount
            humanoid.Health = newHealth
            wait(cooldown)
            canHeal = true
            game.Workspace.HealthPack.Transparency = 0 

        end
    end
end

healthPack.Touched:connect(handleTouch)


while true do
    local currentRotationX = game.Workspace.HealthPack.Rotation.X
    --local currentRotationX = game.Workspace.HealthPack.Rotation.Y
    local currentRotationZ = game.Workspace.HealthPack.Rotation.Z

    game.Workspace.HealthPack.Rotation.X = currentRotationX + 10
    --game.Workspace.HealthPack.Rotation.Y = currentRotationY + 10  
    game.Workspace.HealthPack.Rotation.Z = currentRotationZ + 10

    wait(.5)
end 

Upvotes: 1

Views: 3831

Answers (2)

Open Pees
Open Pees

Reputation: 13

local runService = game:GetService("RunService")

runService.Heartbeat:Connect(function()
    script.Parent.Orientation += Vector3.new(0,0.2,0)
end)

you could change the y axis (or any other axis) of the part's orientation forever to rotate slowly with runService.Heartbeat (while True do loop but quicker for a smoother rotation).

Upvotes: 0

Patrick Bell
Patrick Bell

Reputation: 769

Try the following code. In order to rotate an object correctly (modifying the rotation property usually doesn't do the trick, it's similar to the position property, it conforms to collisions) you must use CFrame.

local x = 0
while true do
  game.Workspace.HealthPack.CFrame = game.Workspace.HealthPack.CFrame * CFrame.Angles(0, math.rad(x), 0)
  x = x + 1
  wait()
end

Fair disclaimer, I haven't worked with RBX.Lua in a while, so this might not be the best way to do it.

Upvotes: 2

Related Questions