I’m trying to create a baseball bat that you can attack with.
I’ve searched alot to find the solution to it, but I didn’t find anything that could help.
local char = game.Players.LocalPlayer.Character
local WeaponTool = script.Parent
script.Parent.Equipped:Connect(function()
game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
char.Torso.ToolGrip.Part0 = char.Torso
char.Torso.ToolGrip.Part1 = WeaponTool.Weapon
end)
WeaponTool.Unequipped:Connect(function()
game.ReplicatedStorage.DisconnectM6D:FireServer()
end)
When you call local “char” your character hasn’t loaded in yet. One simple fix is replacing “game.Players.LocalPlayer.Character” with game.Players.LocalPlayer:WaitForChild(“Character”)
Character isnt a child of player so that wouldnt work
the solution is: local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
I first want to bring up the point that “Torso” doesn’t exist within R15 character models, thus, you should ensure that you’re testing this script with R6 character models only.
I also want to bring up that “ToolGrip” by default doesn’t exist in Torso, so I can only assume that this instance is being created by the ConnectM6D Remote Event.
An idea I have is to use empty variables and declare them when Equipped is called on the tool. I also want to encourage using print statements to determine where the bug is.
local WeaponTool = script.Parent
local player
local char -- Blank Variable that will be Assigned Later (if still nil)
script.Parent.Equipped:Connect(function()
print("Weapon Equipped")
if char == nil then
player = game:GetService("Players").LocalPlayer
print(player.Name .. " owns the weapon!")
char = player.Character or player.CharacterAdded:Wait()
print(char.Name .. "'s character has been found!")
end -- Assigns Player And Character Variables
game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
local torso = char:FindFirstChild("Torso")
if torso then
print("Torso Exists")
else
print("NO TORSO FOUND")
return
end -- Searches For Torso
local ToolGrip = torso:FindFirstChild("ToolGrip")
if ToolGrip then
print("ToolGrip Found")
else
print("ToolGrip NOT FOUND")
return
end -- Searches for Tool Grip
ToolGrip.Part0 = torso
ToolGrip.Part1 = WeaponTool.Weapon -- Sets Part0 and Part1
end)
WeaponTool.Unequipped:Connect(function()
game.ReplicatedStorage.DisconnectM6D:FireServer()
end)
Obviously, all the prints are for debuggins purposes, and once a clear solution comes, you can remove these checks.