Trying to make a GUI appear and damaging effect when player is below a point

Hello I am new too scripting and I am a modeller. I am trying to make a part rise and when it’s Y is above the player a screen GUI appears and you get damaged.

The issue is that it just doesn’t work. :confused: I am trying to figure out what the issue is, whenever I play the game it says that the Player is not a valid member of the workspace.

I searched a few tutorials but couldn’t really figure it out.

DumbJuiceFlood is the name of the part by the way.

Thanks for the help.

This is basically what I am trying to do:
Screen Shot 2022-07-08 at 8.34.06 am

There are many problems here. First, you’re storing the Y value as a variable meaning it will never change, you should add that to the while loop. Secondly, you have two while loops so only one of them will run. Also for the hit you have no .Touched connection so that won’t run.

What you need is this:

-- Local Script in StarterPlayerScripts

-- Services
local Players = game:GetService("Players")

-- Player
local Player = Players.LocalPlayer

-- Character
local Character = Player.Character or Player.CharacterAdded:Wait()

-- Variable
local isUnder = false

-- Loop
while true do
   local WaterHeight = workspace.DumbJuiceFlood.Position.Y
   local CharacterPosition = Character.PrimaryPart.Position.Y
   if CharacterPosition < WaterHeight then
      GUI.Visible = true
      isUnder = true
   else
      if isUnder then
         GUI.Visible = false
         isUnder = false
      end
   end
   task.wait()
end

This can be handled on the client also instead of a while loop you can look into using RenderStepped. As for the movement for the part you can handle that on the server. Hope this helps.

Sorry for the late response I was quite busy today. Anyway I did what you said but i’m a bit confused on what a few of these things mean like RenderStepped. I tried what you said but it comes up with an error which is Players.cobaysaurus_rex.PlayerScripts.LocalScript:20: attempt to index nil with ‘Position’ - Client - LocalScript:20

StarterPlayerScript script:


ServiceScriptService:
Screen Shot 2022-07-08 at 6.14.15 pm

Thanks for the help!

Well you can change PrimaryPart to the HumanoidRootPart that should work.

local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if not HumanoidRootPart then warn("HumanoidRootPart couldn't be found") continue end
local CharacterPostion = HumanoidRootPart.Position.Y

Also again in the server script you have defined a variable outside of the while loop which means the Y value will never change. You need to add it into the while loop.

while true do
   local WaterHeight = workspace.DumbJuiceFlood.Position.Y
   if WaterHeight < 20 then
      -- ...
   end
end

Here’s how I would go about it

-- Yield until water and the HumanoidRootPart is found
local Water = workspace:WaitForChild("DumbJuiceFlood")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local gui = Player.PlayerGui -- the actual LocalPlayer gui, it will differ from the StarterGui (when the frame will become visible)

while true do
    local WaterHeight = Water.Position.Y
    local CharacterHeight = HumanoidRootPart.Position.Y
    if CharacterHeight < WaterHeight then
        gui.DumbJuiceFloodScreen.Frame.Visible = true
        isUnder = true
    else
        if isUnder then
            gui.DumbJuiceFloodScreen.Frame.Visible = false
            isUnder = false
        end
    end
    task.wait()
end

Using WaitForChild() makes the script wait until the instance with the given name exist it is similar to find first child but will never return nil (if the instance doesn’t exist then it will wait forever), this prevent the script from indexing nil, this prevent issues where when the game loads, not every asset is loaded at the exact same time

This will fix the issue, but WaitForChild doesn’t yield infinitely if it can’t find it within a set amount of time it will error, the reason I use FindFirstChild is because if it does return nil then it will just loop through the while loop again so essentially doing the same thing waitforchild does. WaitForChild is mainly used for when a instance is definitely going to be there.

It’s now come up with a new error which is; Script timeout: exhausted allowed execution time - Client - LocalScript:18
I am not sure if I typed this in wrong though.

Not sure, but the issue here might be the continue after checking for the HumanoidRootPart, that is skipping the rest of the code in the loop, including the task.wait()
a simple fix is just to move the task.wait() at the top of the while true loop