How can I fix this bug?

Hey, so my goal for my script is to add a decal to the wall and to players whenever the player or the wall gets hit by the tomato, however, it is not working. There is no errors in the console to help explain why it is not working.


local Tool = script.Parent
local enabled = true
local THROW_FORCE = math.random(40, 120)
local MIN_THROW_DELAY = 1

local DECAL_ID = "rbxassetid://13510998982"
local IMPACT_SOUND_ID = "rbxassetid://8595980577"
local players = game:GetService("Players")

local canThrow = true

local MouseUpdate = Instance.new("RemoteEvent")
MouseUpdate.Name = "MouseUpdate"
MouseUpdate.Parent = Tool

local currentMousePosition = Vector3.new(0, 0, 0)

MouseUpdate.OnServerEvent:Connect(function(player, mousePos)
currentMousePosition = mousePos
end)

local function applyImpactEffects(hit, hitPosition, hitNormal, character)
if not hit or hit:IsDescendantOf(character) then return end
print("Testing did infact worked")
local decal = Instance.new("Decal")
decal.Texture = DECAL_ID

if hitNormal.X > 0.5 then
	decal.Face = Enum.NormalId.Right
elseif hitNormal.X < -0.5 then
	decal.Face = Enum.NormalId.Left
elseif hitNormal.Y > 0.5 then
	decal.Face = Enum.NormalId.Top
elseif hitNormal.Y < -0.5 then
	decal.Face = Enum.NormalId.Bottom
elseif hitNormal.Z > 0.5 then
	decal.Face = Enum.NormalId.Back
else
	decal.Face = Enum.NormalId.Front
end

decal.Parent = hit
game.Debris:AddItem(decal, 10)

local emitter = Instance.new("ParticleEmitter")
emitter.Rate = 15
emitter.Lifetime = NumberRange.new(0.3, 0.6)
emitter.Speed = NumberRange.new(2, 5)
emitter.Texture = DECAL_ID
emitter.Parent = hit
game.Debris:AddItem(emitter, 1)

local sound = Instance.new("Sound")
sound.SoundId = IMPACT_SOUND_ID
sound.Volume = 0.5
sound.Parent = hit
sound:Play()

if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= character then
	local sound2 = Instance.new("Sound")
	sound2.SoundId = IMPACT_SOUND_ID
	sound2.Volume = 0.5
	sound2.Parent = hit.Parent:FindFirstChild("HumanoidRootPart") or hit
	sound2:Play()
end

end

local function onActivated()
if not enabled or not canThrow then return end
enabled = false
canThrow = false

local character = Tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
local torso = character:FindFirstChild("Torso") or character:FindFirstChild("HumanoidRootPart")
if not humanoid or not torso then return end

local handle = Tool.Handle:Clone()
handle.Anchored = false
handle.CanCollide = true
handle.Parent = workspace
handle.Name = "Handle"

local offsetDistance = 2
local throwDirection = (currentMousePosition - torso.Position).Unit
handle.CFrame = torso.CFrame * CFrame.new(throwDirection * offsetDistance)

handle:ApplyImpulse(throwDirection * THROW_FORCE * handle:GetMass())
handle:SetNetworkOwner(nil)

if Tool.Handle:FindFirstChild("BiteSound") then
	Tool.Handle.BiteSound:Play()
end

handle.Touched:Connect(function(hit)
	local hitNormal = (hit.Position - handle.Position).Unit
	applyImpactEffects(hit, hit.Position, hitNormal, character)
	handle:Destroy()
end)

-- Optional: Setup collision groups if needed, but you can skip this if debugging.
-- local PhysicsService = game:GetService("PhysicsService")
-- PhysicsService:RegisterCollisionGroup("TomatoGroup")
-- handle.CollisionGroup = "TomatoGroup"

if #players:GetPlayers() >= 6 then
	game.Debris:AddItem(handle, 3)
else
	game.Debris:AddItem(handle, 3000)
end
Tool:Destroy()

end

local function onEquipped()
if Tool.Handle:FindFirstChild("EquipSound") then
	Tool.Handle.EquipSound:Play()
end
end

Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)

1 Like

Side Note: it seems like the function applyImpactEffects is not getting executed.

im pretty sure its to do with isdescendantof(character) as your returning the function when your checking if the basepart touched is a descendant of your character

Ah, makes sense now, I’ll see if that works

Update: It still doesn’t work even when it’s removed

is the handle clone is property cantouch set to true as i see the clone doesnt destroy afterwards being touched by a basepart in the Touched event

1 Like

So enable cantouch to true???

yeah basically that enable it to true

Same thing still… This is so annoying!

can you show me your explorer window for your tool since your code looks fully right.Or i will test your code in studio to debug it and comeback soon

yep i figured it out it was such a simple mistake.So the reason is due to you destroying the Tool which means every connection in the script will be disconnected due to everything under the tool be destroyed such as the handle and basically the script.

i suggest creating a bindable event in a different script not inside the Tool object but in serverscriptservice then create the handle flying in the air with its touched effects their.

It’s all good now, I got it to work again