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

Football Not Changing Position When Tool Equipped?

Asked by
Rynappel 212 Moderation Voter
5 years ago
Edited 5 years ago

So I have this football thingy. And I want the football to move down to the players feet when they equip a football tool. Here is my script. What should I change? Is there a better way of doing this? Sorry if the script sucks. Im not very good with CFrame and Positions. Thanks!

script.Parent.Equipped:Connect(function()
    script.Parent.Handle.CFrame = CFrame.new(-22.01, 9, -42.1)
end)

And here is a picture of my StarterPack: https://prnt.sc/pa4fe6

1 answer

Log in to vote
0
Answered by 5 years ago

Basic Information

Improvements

  1. Use Local Variables so that you can simply change a single line when needed rather than writing out things like script.Parent multiple times

  2. Do not set a CFrame to a direct location unless it should always appear there. The football is supposed to be placed relative to the player so you should base the Handle's CFrame on the player whose tool it is.

  3. A Tool is placed in the player's Backpack so I'd suggest using a LocalScript for this as the player controls the equipment, not the server

Suggestions

Tools have properties referring to the Grip but this only relates to the player's RightHand.

Since you want to move the handle to the player's feet, I'd use a Weld to place the Tool's Handle in the expected location

Using a RemoteEvent will cause actions made by the player to tell the server to make changes that all players can see, not just the client the LocalScript is descended under.

Final Product

Make sure that the Tool's RequireHandle Property is set to false

Hierarchy

ReplicatedStorage
    Handle
ServerScriptService
    ServerScript
StarterPack
    Tool (Football)
        LocalScript

LocalScript

local remote = game:GetService("ReplicatedStorage"):WaitForChild("FootballEvent")
local tool = script.Parent

tool.Equipped:Connect(function()
    remote:FireServer("Attach", tool)
end)

tool.Unequipped:Connect(function()
    remote:FireServer("Remove", tool)
end)

The above functions just fire the remote when the player equips and unequips the tool.

Server Script

local repst = game:GetService("ReplicatedStorage")
local remote = Instance.new("RemoteEvent")
remote.Name = "FootballEvent"
remote.Parent = repst

remote.OnServerEvent:Connect(function(player, event, tool)
    if event == "Attach" then
        local char = player.Character
        local root = char:WaitForChild("HumanoidRootPart")

        local handle = repst:WaitForChild("Handle"):Clone()

        local weld = Instance.new("Weld")
        weld.Part0 = root
        weld.Part1 = handle
        weld.C0 = CFrame.new(0, -2.5, -1.5)
        weld.Parent = handle

        handle.Parent = tool
    elseif event == "Remove" then
        local handle = tool:FindFirstChild("Handle")
        if handle then
            handle:Destroy()
        end
    end
end)

The first section creates a new RemoteEvent in the ReplicatedStorage

The second section either adds the handle to the tool with the specified placement ["Attach"] or removes the Handle when the Tool is unequipped (so that the next time it is equipped there is only 1 "Handle" in the Tool.) ["Remove"]

Explanation

The weld.Part0 is set to the player's HumanoidRootPart since this part control's player motion and will not abruptly rotate or move like the LeftFoot might

The weld.C0 property determines the offset of Part1 from Part0 so change this line [Line 16] as necessary

Ad

Answer this question