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

Orientation of object is wrong only first time I call function. How can I fix that?

Asked by 4 years ago

This is code that spaws Soda and attaches it to player. The problem is that first time the function is called it spaws Soda is wrong orientation. Second times it's called it's as intended.

Images for reference: image1, image2

local items = {}

    function items.get(player, item)
        local successful = false
        local object, pos, ang
        local animation = Instance.new("Animation")

        if player.PlayerData.Item.Value == nil then
            --Get item type
            if item == "Soda" then
                object = game:GetService("ServerStorage").Soda
                animation.AnimationId = "http://www.roblox.com/Asset?ID=5162117084"
                pos = CFrame.new(0,-1.3,0)
                ang = Vector3.new(0,0,-90)
            end

            local arm = player.Character["Right Arm"] --Spawning and position
            local clone = object:Clone()
            clone.Anchored = false
            clone.Parent = arm
            player.PlayerData.Item.Value = clone

            clone.CFrame = arm.CFrame:ToWorldSpace(pos)
            clone.Orientation = ang

            local weld = Instance.new("WeldConstraint") --Weld parts
            weld.Parent = clone
            weld.Part0 = arm
            weld.Part1 = clone

            --Animation
            local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
            animationTrack:Play()

            successful = true
        end

        return successful
    end

return items

2 answers

Log in to vote
0
Answered by 4 years ago

If you want to get past the problem of it not working first time what you want to do is call the function first, delete the soda, then call the function again and hopefully the soda should be in the correct place.

0
That doesn't fix the problem. You're suggesting to ignore it ThreeTwigs 0 — 4y
0
No offence by the way ThreeTwigs 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

So I think I figured it out. I swapped right arm for torso and adjusted the position and rotation and it worked. Thanks @Gooncreeper for contribution

Answer this question