I have a hockey stick, where if you click it plays an animation. If the ball hits the blade a “BodyForce” is supposed to be created inside the ball, then creating an artificial shot, instead of using just the animation so it can provide a more powerful shot.
But, for some reason the touched function doesn’t start up at all. An odd thing is that when I keep it undisabled when you start the game, it does work well. However, when i set Disabled to true and undisable it when the player clicks, it doesn’t work, even though the script is not disabled.
I’ve tried to undisable it when the player clicks BEFORE the player plays the animation but it still doesn’t work. Nothing appears in output, even though I added a print.
My script:
debounce = false
local Part = script.Parent
local Debris = game:GetService('Debris')
local magnitude = 150
local timeOfForce = 0.2
function onTouched(hit)
if hit.Name == "ball" and debounce == false then
debounce = true
print("touched")
local direction = (hit.Position - Part.Position).unit
local bodyForce = Instance.new('BodyForce')
bodyForce.force = magnitude * direction
bodyForce.Parent = hit
Debris:AddItem(bodyForce, timeOfForce)
wait(1)
debounce = false
end
end
Part.Touched:connect(onTouched)
First, is this in a localscript or a serverscript?
Second, try printing before your variables, as the first line in the script. if that doesn’t fire then Something very weird is happening.
Third, use :Connect in favor of :connect. The latter is depreciated, so you could be getting some legacy issues.
fourth and least importantly, make your debounce variable a local variable. There’s no reason for you to honestly ever use global variables if you’re not declaring them inside a scope, and really not good practice to use them at all.
You’re using an if statement to check if the hit’s name is ball. Nothing in the hit’s properties is named “ball” so therefore the next bits won’t fire. Try using hit.Parent.Name
Wouldn’t the part that the hockey stick hits be the ball? I see no reason for that to be wrong, if the touched part is in fact the ball then the part’s name is ball. Plus, he said the script worked in a different situation without editing names or code, so that check does return true.
He never said he was looking for an external object, he was looking for the part that specifically touched the stick to be named ball. Again, you never addressed my statement that this check returned true and worked in a seperate situation where no code or names were changed. If you were correct and he was supposed to do hit.Parent.Name then the original code should not have worked in any circumstance at all
The really odd thing is that if i set the script’s “Disabled” to false before I test, it works fine, but I wanted it to only work specifically when a player clicks. However, if I set it to true before I test, and use the mouse click script that set the scripts Disabled to false for a few seconds, it will not work, even though the script clearly isn’t Disabled anymore. I have used other scripts in my game that use the same function, and they work perfectly fine.
Also I used your advice and set it to :Connect, made the debounce variable local, and set the print on the first line, but for some reason the print still does not appear in output. The script is a server script.