Hit Script will not remove other player's health and wont increase their leaderstat

As the title says, my script will not remove the other player’s health who get hit, and will not even increase their own leaderstat for some reason.
Any help is appreciated

local Default_Ball = game.Workspace.DefaultBall
local Tool = script.Parent
local Player = script.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
local leaderstat = Player:WaitForChild("leaderstats")
-- Ball
local Ball = script.Parent.Default -- Change according to ball name
local character = Tool.Parent
-- Sound
local HitSound = Instance.new("Sound")
HitSound.Parent = Ball
HitSound.SoundId = "8060079174" -- Change to a heavy hit if damage is more then 20, if higher then 80 then do a VERY heavy hit.
HitSound.Volume = 10
HitSound.RollOffMaxDistance = 30
HitSound.RollOffMinDistance = 5
-- Settings
local debounce = false

Ball.Touched:Connect(function(hit)
	wait(0.05)
	local enemyplayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if not debounce then
		debounce = true
		enemyplayer.Character.Humanoid.Health = enemyplayer.Character.Humanoid.Health - 14 -- Change according to ball
		leaderstat.Smacks.Value = leaderstat.Smacks.Value + 1
		HitSound:Play()
		wait(2)
		debounce = false
	end
end)
3 Likes

Oops!
It was meant to be a different variable for Ball.
Edit: Didn’t work still…

1 Like

bro did you check the output bc i see that you never defined what the leaderstat variable is
i didnt read the entire script lol

and also you should add if (enemyplayer and not debounce) then because if something that touches the ball isnt a player it will also break

local leaderstat = Player:WaitForChild("leaderstats")

Try adding some prints within the connection to see if the ball is registerig the hits :smiley:

what is this script parented to btw and is it a tool or something

It is parented to a tool, and it does have a handle with welds.

i dont think server scripts will run inside the player

so change lines 3 and 4 so that character is Tool.Parent and the Player variable is Players:GetPlayerFromCharacter(character)

If I were to make it a localscript, it would only effect the client, so if they hit a player and killed them, they will show up dead ONLY on their screen.
Also that script doesn’t work

i know this and also can you tell me what its saying in the output

Workspace.BlueClothesPerson.Default.HitScript:3: attempt to index nil with ‘GetPlayerFromCharacter’

wait, is this 2 different scripts?

because in the script that you posted, theres 2 variables named character

Yeah:

Script that turns on the hit script
local Tool = script.Parent
local Player = script.Parent.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
local HitScript = Tool.HitScript
-- Ball
local Ball = script.Parent.DefaultBall
local character = Tool.Parent
-- Animation
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://13962251515"
-- Settings
local debounce = false -- Cooldown
Animation.Parent = Humanoid.Animator

Tool.Activated:Connect(function(activated)
	local HitAnimPlay = Humanoid:LoadAnimation(Animation)
	if not debounce then
		debounce = true
		HitAnimPlay:Play()
		HitScript.Enabled = true
		wait(2)
		HitScript.Enabled = false
		debounce = false
	end
end)
Hit script
local Default_Ball = game.Workspace.DefaultBall
local Tool = script.Parent
local Player = script.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
local leaderstat = Player:WaitForChild("leaderstats")
-- Ball
local Ball = script.Parent.DefaultBall -- Change according to ball name
local character = Tool.Parent
-- Sound
local HitSound = Instance.new("Sound")
HitSound.Parent = Ball
HitSound.SoundId = "8060079174" -- Change to a heavy hit if damage is more then 20, if higher then 80 then do a VERY heavy hit.
HitSound.Volume = 10
HitSound.RollOffMaxDistance = 30
HitSound.RollOffMinDistance = 5
-- Settings
local debounce = false

Ball.Touched:Connect(function(hit)
	wait(0.05)
	local enemyplayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if not debounce then
		debounce = true
		enemyplayer.Character.Humanoid.Health = enemyplayer.Character.Humanoid.Health - 14 -- Change according to ball
		leaderstat.Smacks.Value = leaderstat.Smacks.Value + 1
		HitSound:Play()
		wait(2)
		debounce = false
	end
end)

The part that give the tool turns on the script “script that turns on the hit script”

I really hate scripts that disable and enable other scripts depending on their state; its just an unreliable and ugly practice. A better way to do this would be to combine both scripts into one and when the tool is activated set a variable called isActivated to true, and inside the Touched event run a check whether isActivated is false if it is then return from the function

try this in the “script that enabled the hit script”

-- put me at line 3
local Player,Character,Humanoid

-- put me at bottom of script
Tool.Equipped:Connect(function()
Character = Tool.Parent
Player = game.Players:GetPlayerFromCharacter(Character)
Humanoid = Character:FindFirstChildOfClass("Humanoid")
end)

edit: also yeah what the guy above me said

More errors: Humanoid is not a valid member of Workspace "Workspace"

uh, why is the tool parented to the workspace?

It isnt, it is parented to ServerStorage, the variable at the top of the hit script is something I forgot to remove but doesn’t do anything

can you tell me what everything is parented to then? because your variables are kinda confusing

image
That’s it…