[REPOST, HAD NO RESPONSE AFTER FINAL REPLY FOR 4 HOURS (please actually get a response, It’s been over 5 hours in total…) OLD POST]
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 Player = script.Parent.Parent.Parent
local Humanoid = Player:WaitForChild("Humanoid")
local leaderstat = Player:WaitForChild("leaderstats")
-- Ball
local Ball = script.Parent.DefaultBall -- Change according to ball name
-- Sound
local HitSound = Instance.new("Sound")
HitSound.Parent = Ball
HitSound.SoundId = "rbxasset://sounds//short spring sound.wav" -- 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 - 10
leaderstat.Smacks.Value = leaderstat.Smacks.Value + 1
HitSound:Play()
wait(1.5)
debounce = false
end
end)
i suggest re-scripting the thing then editing this post with the new code as your current code is kinda confusing and it was 1 am so i went to sleep lol
examples of it being confusing
2 variables named character
script.Parent being used multiple times although script.Parent has a variable (Tool)
you only using WaitForChild() and FindFirstChild() once even tho there are many things you have to look for
local Player = script.Parent.Parent - where is this script even sopposed to be running from lol
The simple way to debug any issue like this is to validate that your touch event is being triggered and to print and validate the variables in the function. Really simple, quick, self help you can do yourself.
However, some handholding to assist:
-- This block of variables all depend on how and where the Tool is given to the player
-- I'd recommend only setting these values with a Equipped and Unequip event
-- Also recommend not confusing Character and character, and other core objects
local player
local character
local humanoid
local leaderstat
Ball.Touched:Connect(function(hit)
print("BALL HIT", hit, hit.Parent) -- Confirms a hit, confirms hit Parent. Simple
wait(0.05)
local enemyplayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
print("HIT ENEMY", enemyplayer)
if not debounce then
debounce = true
local enemyCharacter = enemyplayer.Character
local enemyHealth = enemyCharacter.Humanoid.Health
print("Got Char & Health?", enemyCharacter, enemyHealth)
enemyHealth -= 14-- Change according to ball
leaderstat.Smacks.Value = leaderstat.Smacks.Value + 1
HitSound:Play()
wait(2)
debounce = false
end
end)
local function Equipped()
character = Tool.Parent
player = Players:GetPlayerFromCharacter(character)
humanoid = character:FindFirstChild("Humanoid")
leaderstat = player:FindFirstChild("leaderstat ")
end
Tool.Equipped:Connect(Equipped)
Can’t guarantee it will work, but at least it will start to point in the right direction to help yourself
The object ball has canTouch enabled.
I recommend you use operators like += or -= because it’s better and less confusing.
I don’t know why you need to debounce but its’ value doesn’t change so it’s not needed.
There are many variables that are not used, so delete them to avoid confusion.
I edited it and it should be easier to use, as for the script.Parent.Parent.Parent, the script is disabled then turns enabled later when needed, it goes like:
Script > Tool > Backpack > Player
Looking at it, you need more checks to make sure the character you hit is an actual character and it belongs to an enemy.
If you don’t mind, I’m also going to improve the script a bit:
local players = game:GetService("Players")
local player = script.Parent.Parent.Parent
local leaderstats = player:WaitForChild("leaderstats")
local smacksValue = leaderstats:WaitForChild("Smacks")
local ball = Tool:WaitForChild("DefaultBall") -- Change accordingly
local hitSound = Instance.new("Sound")
hitSound.SoundId = "rbxasset://sounds//short spring sound.wav" -- Are you sure this is a real ID?
hitSound.Volume = 10
hitSound.RollOffMaxDistance = 30
hitSound.RollOffMinDistance = 5
hitSound.Playing = false
hitSound.Parent = ball
local debounce = false
local function ballTouched(hit)
if not debounce then
debounce = true
local hitChar = hit:FindFirstAncestorWhichIsA("Model")
local hitPlayer = players:GetPlayerFromCharacter(hitChar)
if hitPlayer and hitPlayer ~= player then
hitPlayer.Character:WaitForChild("Humanoid"):TakeDamage(10)
smacksValue.Value += 1
hitSound:Play()
end
local function debounceDelay() debounce = false end
task.delay(1.5, debounceDelay)
end
end
ball.Touched:Connect(ballTouched)
This also didn’t work for some reason, I will send the other 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
print("got variables")
Tool.Activated:Connect(function(activated)
local HitAnimPlay = Humanoid:LoadAnimation(Animation)
print("activated")
if not debounce then
debounce = true
HitAnimPlay:Play()
HitScript.Enabled = true
print("yeah")
wait(1.5)
HitScript.Enabled = false
debounce = false
end
end)
Looks like the tool is parented to the char, and not the Backpack when the script is enabled. Try this:
local players = game:GetService("Players")
local player = Tool.Parent
local leaderstats = player:WaitForChild("leaderstats")
local smacksValue = leaderstats:WaitForChild("Smacks")
local ball = Tool:WaitForChild("DefaultBall") -- Change accordingly
local hitSound = Instance.new("Sound")
hitSound.SoundId = "rbxasset://sounds//short spring sound.wav" -- Are you sure this is a real ID?
hitSound.Volume = 10
hitSound.RollOffMaxDistance = 30
hitSound.RollOffMinDistance = 5
hitSound.Playing = false
hitSound.Parent = ball
local debounce = false
local function ballTouched(hit)
if not debounce then
debounce = true
local hitChar = hit:FindFirstAncestorWhichIsA("Model")
local hitPlayer = players:GetPlayerFromCharacter(hitChar)
if hitPlayer and hitPlayer ~= player then
print("Player found") -- Make sure this prints
hitPlayer.Character:WaitForChild("Humanoid"):TakeDamage(10)
smacksValue.Value += 1
hitSound:Play()
end
local function debounceDelay() debounce = false end
task.delay(1.5, debounceDelay)
end
end
ball.Touched:Connect(ballTouched)
I will try the script out The sound id does work, it is inside of the game files and plays without a problem.
Edit: The script didn’t work, nothing printed.
Oh, looks like I messed up the player variable. Let me fix that real quick:
local players = game:GetService("Players")
local char = Tool.Parent
local player = players:GetPlayerFromCharacter(char)
if not player then error("Player holding ball not found, char is: " .. char.Name) end
local leaderstats = player:WaitForChild("leaderstats")
local smacksValue = leaderstats:WaitForChild("Smacks")
local ball = Tool:WaitForChild("DefaultBall") -- Change accordingly
local hitSound = Instance.new("Sound")
hitSound.SoundId = "rbxasset://sounds//short spring sound.wav"
hitSound.Volume = 10
hitSound.RollOffMaxDistance = 30
hitSound.RollOffMinDistance = 5
hitSound.Playing = false
hitSound.Parent = ball
local debounce = false
local function ballTouched(hit)
if not debounce then
debounce = true
local hitChar = hit:FindFirstAncestorWhichIsA("Model")
if not hitChar then return end
local hitPlayer = players:GetPlayerFromCharacter(hitChar)
if hitPlayer and hitPlayer ~= player then
print("Player found") -- Make sure this prints
hitPlayer.Character:WaitForChild("Humanoid"):TakeDamage(10)
smacksValue.Value += 1
hitSound:Play()
end
local function debounceDelay() debounce = false end
task.delay(1.5, debounceDelay)
end
end
ball.Touched:Connect(ballTouched)
Let me know if any console warnings or errors appear!
I tried it in single player, and it worked, but second time with 2 players it didnt work.
I suspect it might be the 2 tools instead of 1
Edit: Not the 2 tools, it was the multiplayer??
I think it’s because of the debounce. Let me fix that:
local players = game:GetService("Players")
local char = Tool.Parent
local player = players:GetPlayerFromCharacter(char)
if not player then error("Player holding ball not found, char is: " .. char.Name) end
local leaderstats = player:WaitForChild("leaderstats")
local smacksValue = leaderstats:WaitForChild("Smacks")
local ball = Tool:WaitForChild("DefaultBall") -- Change accordingly
local hitSound = Instance.new("Sound")
hitSound.SoundId = "rbxasset://sounds//short spring sound.wav"
hitSound.Volume = 10
hitSound.RollOffMaxDistance = 30
hitSound.RollOffMinDistance = 5
hitSound.Playing = false
hitSound.Parent = ball
local debounce = false
local function ballTouched(hit)
if not debounce then
local hitChar = hit:FindFirstAncestorWhichIsA("Model")
if hitChar then
local hitPlayer = players:GetPlayerFromCharacter(hitChar)
if hitPlayer and hitPlayer ~= player then
print("Player found")
debounce = true
hitPlayer.Character:WaitForChild("Humanoid"):TakeDamage(10)
smacksValue.Value += 1
hitSound:Play()
local function debounceDelay() debounce = false end
task.delay(1.5, debounceDelay)
end
end
end
end
ball.Touched:Connect(ballTouched)