Change player (created) value when part is touched

Hello !
(sorry for my bad english)
I’m actually doing a specific DeathScreen for specific situation:
For example, if someone die in a supermarket (for example), his death screen will be different if he die in a forest (for example).

When a player is added, a value called DeathMessage is created (it work)

When the player touch a part, his DeathMessage changed (and this doesn’t work)

Here is the script I’ve tried:

script.Parent.Touched:Connect(function(hit)
if hit.Parent ~= nil then
  if hit.Parent:findFirstChild("Humanoid") ~= nil then 
  hit.Parent:findFirstChild("Humanoid").Health  = hit.Parent:FindFirstChild("Humanoid").Health - 100
  hit.Parent:WaitForChild("Settings"):WaitForChild("DeathMessage").Value = "You are jailed in the Supermarket."
  wait(3)
 hit.Parent.Settings:WaitForChild("DeathMessage").Value = "You fail the experience."
 end
 end
end)

I also tried with LocalScript but when I just put a “print()” it didn’t work.

Help me please !!
Thanks for every answers :smiley:

1 Like

The DeathMessage does not change because you are trying to do it from a LocalScript, in order to do this you will need to have a RemoteEvent in ReplicatedStorage and you will need to fire a ServerScript in order to change the value.

Change findFirstChild to FindFirstChild,
do

script.Parent.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChild("Humanoid") then
    hit.Parent.Humanoid.Health = 0 --No need to check for the child, you already are doing an if statement to see if it is there
    local s = hit.Parent:WaitForChild("Settings") --Much more efficent
    local d = s:WaitForChild("DeathMessage") --Again, more efficient 
    d.Value = "You are jailed in the Supermarket." --Don't find first child for each part, just make a variable
    wait(3)
    d.Value = "You fail the experience." --again, a variable
  end
end)

That code is very messy, I cleaned it and changed it for you. What I did should work.
I do suggest doing what @ThousandDegreeKnife mentioned, but if you cannot do it let me know, I can program you a quick script to do what TDK has mentioned.

Edited once more, to make it a bit more efficent and working, if it isn’t working let me know, this should work, keep in mind this is a very basic script and way to do it, and I do not recommend doing it. If you need help, here are some sources;

  1. Remote events & More goodies
  1. Touch event
  1. Changed event ( recommended since you are using values. )

If you need any more help, let me know!

1 Like

You have an extra end that you don’t need.

1 Like

Woops didn’t notice that, sorry I am not on studio currently. Thanks!

1 Like

I tried the script that you gives me and it print : “Infinite yield possible on ‘Workspace.mist0s:WaitForChild(“Settings”)’” but the Settings is in the Players.(local player)
And thanks for the answer !!!

Infinite yield means that the script cannot find “Settings”, have you confirmed that the object is in the correct path?

Here’s a super simple way of scripting a Death Screen.

Server Script inside the Kill Part

local replicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.Touched:Connect(function(hit)
	local hum
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then -- Ensure the player is an actual player
		hum = hit.Parent.Humanoid
		if hum.Health > 0 then -- If the player is not dead
			hum.Health = 0
			replicatedStorage.RemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent), "You have died in the Supermarket!")
		end
	end
end)

Local Script inside a GUI

local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

replicatedStorage.RemoteEvent.OnClientEvent:Connect(function(deathMessage)
	local textLabel = script.Parent.TextLabel
	textLabel.Text = deathMessage
end)

(Make sure to place a RemoteEvent in ReplicatedStorage)

Here is the place file:

Death Screen.rbxl (18.0 KB)

1 Like

OMG, it work, really thanks.
But I don’t understand how the string “You have died…” can change with the same remote event. If you can, can you explain me ?
Really thanks for answer !

It will not work without a remote event because of filtering enabled.
Read more about FilteringEnabled here:

Not too sure what your question means, hopefully someone else will be able to help you or you can check the Developer Hub regarding remote events.