Problems With .Touched Event

I have a function called shoot player, it clones a warning object onto the floor beneath the player. If the player is touching it after the Touched event is ran, they will get deducted currency (Laughs). However my issue is that the touched event rarley fires with more than one player in the game, if the player is playing the game alone the touched event fires perfectly. I’ve ran prints and came to the conclusion that the .Touched event is not being fired at all (99% of the time) with more than one player, but runs almost every time with only one player. There is only one player touching the part a time.

Shoot Player Function


	local function ShootPlayer()
		local warningTouched = false
		canWork = false
		local warning = game.ReplicatedStorage.Bosses.Warning:Clone()
		warning.Parent = workspace
		local offset = Vector3.new(0, -3, 0)
		warning.CFrame = CFrame.new(char.PrimaryPart.Position) * CFrame.new(offset) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(90))
		shoot:Play()
		walk:Stop()
		wait(0.3)
		game.SoundService.Bosses.WildWest.Shot:Play()
		game.SoundService.Bosses.WildWest.Explosion:Play()
		warning.Touched:Connect(function(hit)
			print("THE THING IS TOCUHED BT" .. hit.Name)
			local playerToBeHit = game.Players:GetPlayerFromCharacter(hit.Parent)
			if playerToBeHit and not warningTouched and playerToBeHit.Name == player.Name then
				warningTouched = true
				table.insert(playersInside, playerToBeHit)
				print("Player Hit By Bullet")
			end
		end)
		for i, playerToDeductFrom in ipairs(playersInside) do
			if playerToDeductFrom.leaderstats.Laughs.Value >= 50 then
				playerToDeductFrom.leaderstats.Laughs.Value -= 50
				print("Deducted")
				game.ReplicatedStorage.RemoteEvents.Notification:FireClient(player,"Hit by Bullet -50 Laughs", 3, true)
			end
			table.remove(playersInside, i)
		end
		task.wait()
		warningTouched = false
		warning:Destroy()
		task.wait()
		shoot:Stop()
		task.wait(1)
		canWork = true
		walk:Play()
	end

Get Richest Player Function

local function GetRichestPlayer()
	local most_coins, richest_player = -1, nil
	local players = Players:GetPlayers()
	for i=1,#players do
		local player = players[i]
		local player_coins = player.leaderstats.Laughs and player.leaderstats.Laughs.Value or nil
		if player_coins and player_coins > most_coins then
			most_coins = player_coins
			richest_player = player
		end
	end

	return most_coins, richest_player
end

How ShootPlayer() is called.

task.spawn(function()
		while wait(math.random(5, 10)) do
			for i=0, math.random(1, 55) do
				spawn(ShootPlayer)
				wait(0.15)
			end
		end
	end)

Warning Part Properties.
image
image

If you need more details, pictures or anything to help resolve the issue, please ask.

It’s possible that the warning object is being destroyed before the Touched event has a chance to fire. The reason for this is that the warning object is being destroyed at the end of the ShootPlayer function, regardless of whether or not the Touched event has actually fired.

One solution to this issue could be to relocate the code that checks for players inside the warning object into the Touched event callback, so that it only runs when the event is triggered. Additionally, you could move the destruction of the warning object into the callback, but after the code that deducts currency from the players.

local function ShootPlayer()
    local warning = game.ReplicatedStorage.Bosses.Warning:Clone()
    warning.Parent = workspace
    local offset = Vector3.new(0, -3, 0)
    warning.CFrame = CFrame.new(char.PrimaryPart.Position) * CFrame.new(offset) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(90))
    shoot:Play()
    walk:Stop()
    wait(0.3)
    game.SoundService.Bosses.WildWest.Shot:Play()
    game.SoundService.Bosses.WildWest.Explosion:Play()
    
    local playersInside = {}
    
    warning.Touched:Connect(function(hit)
        local playerToBeHit = game.Players:GetPlayerFromCharacter(hit.Parent)
        if playerToBeHit and playerToBeHit.Name == player.Name then
            if playerToBeHit.leaderstats.Laughs.Value >= 50 then
                playerToBeHit.leaderstats.Laughs.Value -= 50
                print("Deducted")
                game.ReplicatedStorage.RemoteEvents.Notification:FireClient(player,"Hit by Bullet -50 Laughs", 3, true)
            end
            table.insert(playersInside, playerToBeHit)
            print("Player Hit By Bullet")
        end
    end)
    
    wait(1)
    warning:Destroy()
    
    shoot:Stop()
    walk:Play()
end

Let me know how it goes!

Ah, that makes so much more sense. It works! Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.