Help on bat script

I have been having problems with this bat script, I do not know how to fix it.

I came on the devfourm to ask for help.

LocalScript:
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)

Script:
wait(0.1)

local plr = script.Parent.Parent.Parent

local char = plr.Character

local idle = char.Humanoid:LoadAnimation(script.Animations.Idle)

local equipped = false

local attacking = false

local RaycastHitbox = require(game.ServerScriptService.RaycastHitboxV2)

local Hitbox = RaycastHitbox:Initialize(script.Parent.Weapon)

durability = 7

local attack1 = char.Humanoid:LoadAnimation(script.Animations.Attack1)

local broken = false

Hitbox.OnHit:Connect(function(hit, humanoid)

if humanoid.Parent ~= char and humanoid.Parent.Handler.Infected.Value == true then

script.Parent.Weapon.Hit:Play()

local damage = math.random(15,35)

local Speed = 25

local Force = 40000

local TotalForce = Force

local KnockBack = Instance.new(“BodyVelocity”,hit.Parent:FindFirstChild(“Torso”))

KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)

KnockBack.Velocity = plr.Character:FindFirstChild(“HumanoidRootPart”).CFrame.LookVector * Speed – based on the direction YOUR character is facing.

game.Debris:AddItem(KnockBack,0.1)

humanoid:TakeDamage(damage)

humanoid.Parent.Actions.Stunned.Value = true

if durability == 0 then

Hitbox:HitStop()

script.Parent.Weapon.Transparency = 1

script.Parent.Weapon.B:Play()

script.Parent.Weapon.ParticleEmitter:Emit(50)

game.ReplicatedStorage.EditGUI:FireClient(plr,“text”,“Your baseball bat broke.”)

broken = true

idle:Stop()

attack1:Stop()

wait(2)

script.Parent:Destroy()

end

durability = durability - 1

end

end)

script.Parent.Equipped:Connect(function()

if not plr.Character.Actions.Stunned.Value == true and not broken then

idle:Play()

equipped = true

else

plr.Character.Humanoid:UnequipTools()

end

end)

script.Parent.Unequipped:Connect(function()

idle:Stop()

equipped = false

end)

script.Parent.Activated:Connect(function()

if not attacking and equipped and not plr.Character.Actions.Stunned.Value == true and durability >= 0 then

attack1:Play()

attacking = true

script.Parent.Weapon.Prepare:Play()

wait(0.16)

Hitbox:HitStart()

script.Parent.Weapon.Swing:Play()

wait(0.4)

Hitbox:HitStop()

wait(2)

attacking = false

wait(0.4)

end

end)

plr.Character.Actions.Stunned.Changed:Connect(function()

plr.Character.Humanoid:UnequipTools()

end)
DM me on discord if you need to contact me : Someone else#3350

Screenshots below
|
|
/
download - 2021-01-09T212837.120

1 Like

I am trying to see what was actually going wrong for you.
Your script in the post does not seem to be formatted correctly. Did you put ``` in front and back of it so it formats correctly?
Have you tried putting prints into the script to check the logic is flowing as you expect?
Do you get any error messages?
Please help us to help you.

I am having trouble formatting the script, can you contact me on discord so I can show you the script?
Discord: Someone else#3350

local script?

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)

server script?

wait(0.1)
local plr = script.Parent.Parent.Parent
local char = plr.Character
local idle = char.Humanoid:LoadAnimation(script.Animations.Idle)
local equipped = false
local attacking = false
local RaycastHitbox = require(game.ServerScriptService.RaycastHitboxV2)
local Hitbox = RaycastHitbox:Initialize(script.Parent.Weapon)
durability = 7
local attack1 = char.Humanoid:LoadAnimation(script.Animations.Attack1)
local broken = false
Hitbox.OnHit:Connect(function(hit, humanoid)
    if humanoid.Parent ~= char and humanoid.Parent.Handler.Infected.Value == true then
        script.Parent.Weapon.Hit:Play()
        local damage = math.random(15,35)
        local Speed = 25
        local Force = 40000
        local TotalForce = Force
        local KnockBack = Instance.new(“BodyVelocity”,hit.Parent:FindFirstChild(“Torso”))
        KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)
        KnockBack.Velocity = plr.Character:FindFirstChild(“HumanoidRootPart”).CFrame.LookVector * Speed – based on the direction YOUR character is facing.
        game.Debris:AddItem(KnockBack,0.1)
        humanoid:TakeDamage(damage)
        humanoid.Parent.Actions.Stunned.Value = true
        if durability == 0 then
            Hitbox:HitStop()
            script.Parent.Weapon.Transparency = 1
            script.Parent.Weapon.B:Play()
            script.Parent.Weapon.ParticleEmitter:Emit(50)
            game.ReplicatedStorage.EditGUI:FireClient(plr,“text”,“Your baseball bat broke.”)
            broken = true
            idle:Stop()
            attack1:Stop()
            wait(2)
            script.Parent:Destroy()
        end
        durability = durability - 1
    end
end)

script.Parent.Equipped:Connect(function()
    if not plr.Character.Actions.Stunned.Value == true and not broken then
        idle:Play()
        equipped = true
    else
        plr.Character.Humanoid:UnequipTools()
    end
end)

script.Parent.Unequipped:Connect(function()
    idle:Stop()
    equipped = false
end)

script.Parent.Activated:Connect(function()
    if not attacking and equipped and not plr.Character.Actions.Stunned.Value == true and durability >= 0 then
        attack1:Play()
        attacking = true
        script.Parent.Weapon.Prepare:Play()
        wait(0.16)
        Hitbox:HitStart()
        script.Parent.Weapon.Swing:Play()
        wait(0.4)
        Hitbox:HitStop()
        wait(2)
        attacking = false
        wait(0.4)
    end
end)

plr.Character.Actions.Stunned.Changed:Connect(function()
    plr.Character.Humanoid:UnequipTools()
end)

I just copied and pasted and put it through a prityfier.

I used VSCode

1 Like

Yes that is correct. I’m not sure why it did not format for me, sorry.

This will always be like 50 50. Make sure to wrap this in a repeat until conditional.

Part0 is usually the origin but thats petty

instead of yielding the thread have the plr be more implicit. How are you getting the player anyways I don’t think you can get user data on the server

To add on you should really be checking the theta of the two vectors (distance vector and look vector) instead of using a raycast. very expensive but i understand if u dont cause you will have to remake your system