Touched not working (script)

script:

 script.Parent.Touched:Connect(function(plr)

	local ui = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Die")
	local menu = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Menu")

	print("hit")

	script.Parent.Gunshot:Play()

	local character = plr.Character or plr.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")

	humanoid.Health = 0

	ui.Visible = true

	wait(0.5)

	menu.Visible = true

	print("everything good")
	print("HOPE")

end)

located inside part
located inside workspace
located inside game

You should use Remote events tell the client to handle anything UI related, instead of changing it on the server.

Oh oops I forgot. .Touched returns the hit, not the player the correct script would be:

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)

local humanoid = char:FindFirstChild("Humanoid")
-- over here, I would recommend for you to use remote events to change the ui, but it doesn't really matter.

	local ui = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Die")
	local menu = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Menu")

	print("hit")

	script.Parent.Gunshot:Play()

	humanoid.Health = 0

	ui.Visible = true

	wait(0.5)

	menu.Visible = true

	print("everything good")
	print("HOPE")
end
end)

Hello! the Touched function doesn’t return the player, instead it return the object that touched it.

Thats why i do plr.Character.Humanoui

I gave u a script, test that out. Sorry about the layout lol

This isn’t how it works, you need to verify if the hit parent have a humanoid, the touched is returning a part of the player.

It doesn’t return the player, it returns whatever it hit. So if it hit your rightfoot, it would return rightfoot.

char is an unknown global ;;;;;;;;;;;;

But I defined char… local char = hit.Parent

You defined it after the GetPlayerFromCharacter

Oops sorry, get rid of the player line. I accidentally defined it before.

Workspace.Robber.Script:9: attempt to index nil with ‘WaitForChild’

I edited it, try now ;;;;;;;;;;;;

ok btw we need a um like um you kniow

Oh yeah u need it sorry. Lemme change it again XD

Ok try now ;;;;;;;;;;;;;;;;;;;;;

1 Like

thank you bro. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

local players = game:GetService("Players")
local part = script.Parent
local gunshot = part.Gunshot

part.Touched:Connect(function(hit)
	local player = players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local character = hit.Parent
		local humanoid = character.Humanoid
		local playergui = player.PlayerGui
		local screengui = playergui.ScreenGui
		local diegui = screengui.Die
		local menugui = screengui.Menu

		gunshot:Play()
		humanoid.Health = 0
		diegui.Visible = true
		task.wait(0.5)
		menugui.Visible = true
	end
end)
1 Like