Black Screen Script Not Working

I’m currently attempting to make a script that detects when a player touches a specific part it turns your screen black, waits 2 seconds, and then kills you. I finished making the script but it doesn’t work in any way at all. I’m wondering exactly why the script does not work and how I would go about fixing it.

Script:

local shadow = script.Parent

shadow.Touched:Connect(function(hit)

	if hit.Parent:FindFirstChild("Humanoid") == true then

		print("Player has touched shadow")

		local Gui = game.Players.LocalPlayer.PlayerGui.Void
		Gui.Enabled = true
		wait(2)
		local Humanoid = game.Players.LocalPlayer.Character.Humanoid
		local Health = Humanoid.Health
		Health = 0

	end
end)

Edit: Got the script working with the help of VoidedBlades and MediaHQ. Thank You for the help!

I had to change the == value to ~= and move the script into starter player scripts Final Result:

local shadow = game.Workspace.TheShadow

shadow.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") ~= nil then

print("Player has touched shadow")

local Gui = game.Players.LocalPlayer.PlayerGui.Void
Gui.Enabled = true
wait(2)
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
Humanoid.Health = 0

end
	end)

FindFirstChild returns an instance or nil, not a boolean. that’s where it doesn’t pass through.

You can fix this by changing the == true to ~= nil or just remove == true completely.

Alongside of this it’s not efficient to make a seperate value for a reference to Humanoid.Health, instead just adjust it from the Humanoid value you’ve created as Humanoid.Health = 0

2 Likes

LocalPlayer can only be used in LocalScripts, so you’ll have to create a LocalScript to handle the Touched event. You can use Humanoid | Roblox Creator Documentation for the LocalPlayer’s character to detect when it has touched the specific part.

If you want to stick with using a server script, another option is to use Players | Roblox Creator Documentation when finding the player that touched the part, and then setting their ScreenGui to Enabled

I’ve changed the script to what is shown below but it seems like it still doesn’t work, is there a reason for this?

local shadow = script.Parent

shadow.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") ~= nil then

print("Player has touched shadow")

local Gui = game.Players.LocalPlayer.PlayerGui.Void
Gui.Enabled = true
wait(2)
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
Humanoid.Health = 0

end
	end)

Is this in a localscript or a normal script? You can also get the player using

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

Like what @mymommakesbathbombs4 explained

The script is currently a Local Script that is under the part that the player is to touch

Localscripts can’t run in the workspace. Try setting the parent of the localscript to one of the following below


https://developer.roblox.com/en-us/api-reference/class/LocalScript

1 Like

I’m not 100% sure how to go about doing this but I attempted to use the method with what I know and came up with what’s shown below but this also doesn’t seem to work

local shadow = script.Parent
local Humanoid = game.Players.LocalPlayer.Character.Humanoid

Humanoid.Touched:Connect(function(hit)

if hit == shadow then

		print("Player has touched shadow")

		local Gui = game.Players.LocalPlayer.PlayerGui.Void
		Gui.Enabled = true
		wait(2)
		local Humanoid = game.Players.LocalPlayer.Character.Humanoid
		Humanoid.Health = 0

end

end)

Try putting the LocalScript in the ScreenGui, and change the path for local shadow to its correct location. You might want to use WaitForChild() for local shadow as well

1 Like

Instead of creating a script for each player why don’t you just have 1 script in the actual kill part?