My goal for this script is whenever a player or a humanoid object gets hit with a pizza, a decal on their avatar should pop up on their torso. However, it is not appearing in this case. There is no errors to help explain why it is not working.
local Tool = script.Parent
local enabled = true
local THROW_FORCE = 60
local MIN_THROW_DELAY = 1
local DECAL_ID = "rbxassetid://9117409728"
local IMPACT_SOUND_ID = "rbxassetid://8595980577"
local canThrow = true
local MouseUpdate = Instance.new("RemoteEvent")
MouseUpdate.Name = "MouseUpdate"
MouseUpdate.Parent = Tool
local currentMousePosition = Vector3.new(0,0,0) --Default position
MouseUpdate.OnServerEvent:Connect(function(player, mousePos)
currentMousePosition = mousePos
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")
if not humanoid then return end
local torso = character:FindFirstChild("Torso") or character:FindFirstChild("HumanoidRootPart")
if not torso then return end
local handle = Tool.Handle:Clone()
handle.Anchored = false
handle.CanCollide = true
handle.Parent = workspace
handle.CFrame = Tool.Handle.CFrame
-- Apply throwing force using the *latest* mouse position
local throwDirection = (currentMousePosition - handle.Position).Unit
handle:ApplyImpulse(throwDirection * THROW_FORCE * handle:GetMass())
handle:SetNetworkOwner(nil)
Tool.Enabled = false
if Tool.Handle:FindFirstChild("BiteSound") then
Tool.Handle.BiteSound:Play()
end
local function applyImpactEffects(hit)
if hit and hit.Parent and hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= character then
local decal = Instance.new("Decal")
decal.Texture = DECAL_ID
decal.Face = Enum.NormalId.Front
decal.Parent = hit
game.Debris:AddItem(decal, 5)
local emitter = Instance.new("ParticleEmitter")
emitter.Parent = hit
emitter.Rate = 10
emitter.Lifetime = NumberRange.new(0.5, 1)
game.Debris:AddItem(emitter, 1)
local sound = Instance.new("Sound")
sound.SoundId = IMPACT_SOUND_ID
sound.Parent = hit
sound.Volume = 0.5
sound:Play()
local sound2 = Instance.new("Sound")
sound2.SoundId = IMPACT_SOUND_ID
sound2.Parent = hit.Parent.HumanoidRootPart
sound2.Volume = 0.5
sound2:Play()
end
end
local connection = handle.Touched:Connect(applyImpactEffects)
game.Debris:AddItem(handle, 3)
wait(MIN_THROW_DELAY)
if connection then connection:Disconnect() end
Tool.Enabled = true
enabled = true
canThrow = true
end
local function onEquipped()
if Tool.Handle:FindFirstChild("EquipSound") then
Tool.Handle.EquipSound:Play()
end
end
Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)
Video: