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

How can I move a part's position to the HumanoidRootPart?

Asked by
Kegani 31
6 years ago

Hello

function onClick(noot)
    local val = script.Parent.Parent.TextBox.Value.Value
    local text = script.Parent.Parent.TextBox
    val = text.Text
    local plr = game.Players:FindFirstChild(val)
    local val2 = script.Parent.Parent.dis.Value.Value
    local text2 = script.Parent.Parent.dis
    val2 = text2.Text
    local item = game.ServerStorage.SponsorItems:FindFirstChild(val2)
    if plr and plr.Character and plr.Character:FindFirstChild("Arm1") and item then
        if val2 == "Food" then
            local foood = game.ServerStorage.SponsorItems.Food:getChildren()
            -- local clone = foood[math.random(1,#foood)]:Clone()
            local e = plr.Character.HumanoidRootPart.Position
            local ab = Vector3.new(e.x, e.y, e.z)
            for i,v in pairs(e) do
                game.Workspace.Sponsor.Position = v
            end
        end
    end
end

I'm currently trying to move the part's position to the plr's HumanoidRootPart, but I just don't know how to. I previously tried using another for loop by doing that:

            for i = 1, ab, .5 do
                game.Workspace.Sponsor.Position = game.Workspace.Sponsor.Position + i
            end

I however replaced it with the i,v in pairs thing because it stated ab should be a number, or something like that.

While we're at it, do you guys know how I could give a player a tool when the "Sponsor" part touches the HumanoidRootPart?

Thanks

0
I posted an updated aswser! OFanTDMO 50 — 6y

2 answers

Log in to vote
0
Answered by
dispeller 200 Moderation Voter
6 years ago

Hey there!

Judging from your comments, I see that you don't want to set a position, but you want to do what is commonly referred to as "tweening".

There is this cool thing called TweenService, and it makes life a lot easier.

Let me show you how it is done.

local TweenService = game:GetService("TweenService")

local sponsorPart = game.Workspace.Sponsor

local goal = {}
goal.Position = Vector3.new(10,20,30)

local tweenInfo = TweenInfo.new(5) -- time it takes for the tween to happen

local tween = TweenService:Create(sponsorPart, tweenInfo, goal)

tween:Play()

Alternatively, if you wanted to do what you were trying to do before,one easy to understand way to do it would be this:

local OldCFrame = game.Workspace.Sponsor.CFrame
for i = 1, ab, .5 do
    game.Workspace.Sponsor.CFrame = CFrame.new(OldCFrame.X + i,OldCFrame.Y,OldCFrame .Z)
end

and you can also do this, but if this confuses you, ignore it.

I know it is a little weird to multiply when you are trying to add, but that is how CFrame works.

for i = 1, ab, .5 do
    game.Workspace.Sponsor.CFrame = game.Workspace.Sponsor.CFrame*CFrame.new(1,0,0)
end
0
Thank you! Kegani 31 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Alright, if I'm not mistaken your part variable is Sponsor; You should use CFrame instead of position:

Sponsor.CFrame = plr.Character.HumanoidRootPart.CFrame -- * CFrame.new(0, 0, 0) can be offset

For the tool: Insert or clone the tool into Sponsor, then add a script / clone a script into sponsor

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local tool = script.Parent.TOOL_NAME:Clone
        tool.Parent = game.Players:FindFirstChild(hit.Parent.Name).Backpack
    end
end)

EDIT:

You should use TewwnService or bodyposition. I don't know much about tween service but I know it has easing. Script for body position:

local bp = Instance.new("BodyPosition", Sponsor)
bp.Position = plr.Character.HumanoidRootPart.Position
Sponsor.Anchored = false
bp.ReachedPosition:connect(function()
    --Rest of code
end)

Edited tool script: script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then if hit.Parent:FindFirstChild("TOOL_NAME") ~= nil and game.Players:FindFirstChild(hit.Parent.Name):FindFirstChild("Tool_") ~= nil then local tool = script.Parent.TOOL_NAME:Clone tool.Parent = game.Players:FindFirstChild(hit.Parent.Name).Backpack end end end)

0
Hello again, thank you for your answer which literally saved my life, however, do you know how Sponsor could move smoothly like a GUI would with UDim2 ? Kegani 31 — 6y
0
Also forgot to include in the last comment if you'd know how to make the tool clone only once. Kegani 31 — 6y

Answer this question