I need help with my script

I am trying to make a script where there is a 30 second timer and you need to get into a room and if you are not in that room you die here is my current script

local player = game.Players.LocalPlayer
local TimerEvent = game.ReplicatedStorage.TimerEvent
local ScreenGui = player.PlayerGui.ScreenGui

local randomCharacter
local randomCharacterName

local function getRandomCharacter()
local players = game.Players:GetPlayers()
repeat
local number = math.random(1,#players)
local randomPlayer = players[number]
randomCharacter = randomPlayer.Character
until randomCharacter

randomCharacterName = randomCharacter.Name
randomCharacter.Archivable = true

end

local function killPlayer()
– Modify this function to handle the player’s death however you want
player.Character:BreakJoints()
end

local stopTimer = false

TimerEvent.OnClientEvent:Connect(function(timer, isInRoom)
ScreenGui.TimerFrame.TextLabel.Text = timer
ScreenGui.TimerFrame.TextLabel.Visible = true
wait(1)

repeat
	timer = timer - 1
	ScreenGui.TimerFrame.TextLabel.Text = timer
	wait(1)
until timer <= 0 or stopTimer

ScreenGui.TimerFrame.TextLabel.Visible = false

if not isInRoom then
	killPlayer()
end

stopTimer = false

end)

local function checkRoom()
local isInRoom = false

local offset = Vector3.new(, 0, 0)

local roomCenter = roomPart.Position + offset
local roomExtents = roomSize / 2

local playerPosition = character.HumanoidRootPart.Position

-- Check if the player's position is within the room's boundaries
if math.abs(playerPosition.X - roomCenter.X) <= roomExtents.X and
	math.abs(playerPosition.Y - roomCenter.Y) <= roomExtents.Y and
	math.abs(playerPosition.Z - roomCenter.Z) <= roomExtents.Z then
	isInRoom = true
end

end
– Replace the following lines with your actual implementation
local character = player.Character
if character then
– Replace “Bedrooms” with the name or identifier of the designated room
if character:FindFirstChild(“Bedrooms”) then
isInRoom = true
end
end

TimerEvent:FireServer(30, isInRoom)

end

please review it and tell me what is wrong so I can improve in the future

1 Like

Hey could you please edit your post and include all the code in a code block by putting ``` infront and behind it and removing the random code blocks?

1 Like
local player = game.Players.LocalPlayer
local TimerEvent = game.ReplicatedStorage.TimerEvent
local ScreenGui = player.PlayerGui.ScreenGui

local randomCharacter
local randomCharacterName

local function getRandomCharacter()
    local players = game.Players:GetPlayers()
    repeat
        local number = math.random(1, #players)
        local randomPlayer = players[number]
        randomCharacter = randomPlayer.Character
    until randomCharacter

    randomCharacterName = randomCharacter.Name
    randomCharacter.Archivable = true
end

local function killPlayer()
    -- Modify this function to handle the player's death however you want
    player.Character:BreakJoints()
end

local stopTimer = false

TimerEvent.OnClientEvent:Connect(function(timer, isInRoom)
    ScreenGui.TimerFrame.TextLabel.Text = timer
    ScreenGui.TimerFrame.TextLabel.Visible = true
    wait(1)

    repeat
        timer = timer - 1
        ScreenGui.TimerFrame.TextLabel.Text = timer
        wait(1)
    until timer <= 0 or stopTimer

    ScreenGui.TimerFrame.TextLabel.Visible = false

    if not isInRoom then
        killPlayer()
    end

    stopTimer = false
end)

local function checkRoom()
    local isInRoom = false

    local offset = Vector3.new(0, 0, 0) -- Set the offset vector to appropriate values

    local roomPart = workspace.Room -- Replace "Room" with the actual room part name
    local roomSize = roomPart.Size

    local roomCenter = roomPart.Position + offset
    local roomExtents = roomSize / 2

    local character = player.Character
    if character then
        -- Replace "Bedrooms" with the name or identifier of the designated room
        if character:FindFirstChild("Bedrooms") then
            isInRoom = true
        end
    end

    TimerEvent:FireServer(30, isInRoom)
end

checkRoom()

and you should do these things NECISSARELY:

  1. The checkRoom function has been updated to properly check if the player’s character is within the designated room. Make sure to replace Room with the actual name of your room part and Bedrooms with the appropriate identifier for the designated room.
  2. The offset vector offset has been set to (0, 0, 0). You should update it with the appropriate values to position the room correctly in your game.
  3. The code now calls the checkRoom function at the end to start the process. If you need to trigger this code based on some event or condition, you can adjust the placement accordingly.

but please put your scripts on a code block next time or else your post will get moderated

2 Likes

There are several issues in your script. Let’s go through them one by one:

  1. In the getRandomCharacter function, the loop to find a random character is incorrect. You should break the loop when a random character is found. Here’s the corrected code:
local function getRandomCharacter()
    local players = game.Players:GetPlayers()
    repeat
        local number = math.random(1, #players)
        local randomPlayer = players[number]
        randomCharacter = randomPlayer.Character
    until randomCharacter
    randomCharacterName = randomCharacter.Name
    randomCharacter.Archivable = true
end

The checkRoom function is missing the implementation of checking if the player is in the designated room. Here’s a corrected version of the function:

local function checkRoom()
    local isInRoom = false

    local offset = Vector3.new(0, 0, 0) -- Adjust the offset based on your room position

    local roomCenter = roomPart.Position + offset
    local roomExtents = roomSize / 2

    local player = game.Players.LocalPlayer
    local character = player.Character

    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            local playerPosition = humanoidRootPart.Position

            -- Check if the player's position is within the room's boundaries
            if math.abs(playerPosition.X - roomCenter.X) <= roomExtents.X and
                math.abs(playerPosition.Y - roomCenter.Y) <= roomExtents.Y and
                math.abs(playerPosition.Z - roomCenter.Z) <= roomExtents.Z then
                isInRoom = true
            end
        end
    end

    TimerEvent:FireServer(30, isInRoom)
end
  1. You need to call the checkRoom function somewhere in your code to start checking if the player is in the designated room. Make sure to call it at the appropriate time, such as when the player enters the room or at a specific event trigger.
  2. In the TimerEvent.OnClientEvent connection, you should set stopTimer to true when the timer reaches 0 to prevent further execution of the loop. Add the following line inside the if not isInRoom then block:
stopTimer = true

Please review and modify your script based on these corrections. Be sure to adjust the code according to your specific game setup, room position, and any additional functionality you require.

1 Like