• Register a free account to view all forum content. Click here to create your own account.

End Game Script help please? :)

AlghaKupo

New Member
Joined
Jan 2, 2019
Messages
3
Roblox
Alfhakupo
okay! so I'm almost done with my game, but I'm having trouble coming up with a script where if all my active contestants touch a part
the game ends giving those contestants a win. I have it set currently where if time runs out and the contestants are still in they win... which you can kinda see the problem because if they reach the area that they are safe in and there is time on the clock people will just be sitting waiting for the game to end. SO!
-I have the "part" in workspace that they have to touch to win but no idea how to script that.
-Most of my scripting is in ServerScriptService
-Here is where I need to put in the script:

local gameresult = "PlayersWin"
if bloxxeractive then
if #activecontestants > 2 then
--Bloxxer Failed
event:FireAllClients("Result", "PlayersWin")

that would mean there is still a person contesting along with the bloxxer so it would give the players the win when time runs out.
#activecontestants = players in the game
bloxxer = who they are facing
my "part" that I want #activecontestants - bloxxer to touch and win is: game.Workspace.Lobby.EscapeWall1.EscapeWall
Do I have to make a table so gathers all active contestants then if it fills with remaining contestants they win. yikes! sorry for being so over-thorough.
 

CallMeKY

Active Member
Joined
Feb 25, 2018
Messages
387
Ratings
115
Roblox
CallMeKY
Discord
CallMeKY#5012
-I have the "part" in workspace that they have to touch to win but no idea how to script that.
Where this is vital to the mechanics of your game, I'd recommend also checking magnitude. People claim that exploiters can send false positives to Touched events. It should look something like this, and could be easily added to your main script.
Code:
local Players = game:GetService('Players')

local Part = script.Parent
local MaxDistance = 15

Part.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then -- check if the touched part is a player
        print(tostring(Hit) .. ' does not belong to any character.')
        return
    end
    local RootPart = Hit.Parent:FindFirstChild('HumanoidRootPart')
    if (RootPart.Position - Part.Position).magnitude <= MaxDistance then
        print('Player is near part and has escaped.')
        -- move player to EscapedPlayers table
    else
        print('Player is not near part, and may be an exploiter.')
    end
end)
Other than that, I'd runs checks on remaining players whenever someone leaves, dies, or escapes. Track escaped players in a table, and remove them from it if they leave. If only the bloxxer remains, and no one escaped, give the bloxxer the win. If only the bloxxer remains but players escaped, give the players the win. If the bloxxer leaves, either restart the match or give players the win.
 
Joined
Jun 4, 2018
Messages
240
Ratings
11
Roblox
proclet
-I have the "part" in workspace that they have to touch to win but no idea how to script that.
Where this is vital to the mechanics of your game, I'd recommend also checking magnitude. People claim that exploiters can send false positives to Touched events. It should look something like this, and could be easily added to your main script.
Code:
local Players = game:GetService('Players')

local Part = script.Parent
local MaxDistance = 15

Part.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then -- check if the touched part is a player
        print(tostring(Hit) .. ' does not belong to any character.')
        return
    end
    local RootPart = Hit.Parent:FindFirstChild('HumanoidRootPart')
    if (RootPart.Position - Part.Position).magnitude <= MaxDistance then
        print('Player is near part and has escaped.')
        -- move player to EscapedPlayers table
    else
        print('Player is not near part, and may be an exploiter.')
    end
end)
Other than that, I'd runs checks on remaining players whenever someone leaves, dies, or escapes. Track escaped players in a table, and remove them from it if they leave. If only the bloxxer remains, and no one escaped, give the bloxxer the win. If only the bloxxer remains but players escaped, give the players the win. If the bloxxer leaves, either restart the match or give players the win.
Hey CallMeKY, I'm trying to make this a bit more fancy, like adding some sort of Gui message instead of the plain "print". Unfortunately, the server is returning all of the players, instead of just the one who has actually touched the part. Any idea why is this happening ?

This is the LocalScript :
Code:
local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")

local deb = Instance.new('BoolValue', ReplicatedStorage)
deb.Name = Player.Name..'_debounce'
deb.Value = true
local debounce = deb.Value
debounce = true

function getPlayerFromCharacter(character)
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player.Character == character then
            return player
        end
    end
end

workspace.touchPart.Touched : connect (function(part)   
    if debounce then
        local plr = getPlayerFromCharacter (part.Parent)
        print (tostring(plr)..'  has touched the red part')
        touchEvent:FireServer()
    end
    debounce = false
end)

local function printPlayerName(juc)
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = tostring(juc)
    Player.PlayerGui.TouchedGui.Enabled  = true
    wait (3)
    Player.PlayerGui.TouchedGui.Enabled  = false
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = ''
end

printEvent.OnClientEvent:Connect(printPlayerName)
And this is the ServerScript :

Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")
 
touchEvent.OnServerEvent:Connect(function(playa)
    print ('server has received this name : '..tostring(playa) ) -- this is where the server is returning all of the players names
    printEvent : FireAllClients(playa)                                        --instead of just the one player who has touched the part
end)
 

CallMeKY

Active Member
Joined
Feb 25, 2018
Messages
387
Ratings
115
Roblox
CallMeKY
Discord
CallMeKY#5012
-I have the "part" in workspace that they have to touch to win but no idea how to script that.
Where this is vital to the mechanics of your game, I'd recommend also checking magnitude. People claim that exploiters can send false positives to Touched events. It should look something like this, and could be easily added to your main script.
Code:
local Players = game:GetService('Players')

local Part = script.Parent
local MaxDistance = 15

Part.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then -- check if the touched part is a player
        print(tostring(Hit) .. ' does not belong to any character.')
        return
    end
    local RootPart = Hit.Parent:FindFirstChild('HumanoidRootPart')
    if (RootPart.Position - Part.Position).magnitude <= MaxDistance then
        print('Player is near part and has escaped.')
        -- move player to EscapedPlayers table
    else
        print('Player is not near part, and may be an exploiter.')
    end
end)
Other than that, I'd runs checks on remaining players whenever someone leaves, dies, or escapes. Track escaped players in a table, and remove them from it if they leave. If only the bloxxer remains, and no one escaped, give the bloxxer the win. If only the bloxxer remains but players escaped, give the players the win. If the bloxxer leaves, either restart the match or give players the win.
Hey CallMeKY, I'm trying to make this a bit more fancy, like adding some sort of Gui message instead of the plain "print". Unfortunately, the server is returning all of the players, instead of just the one who has actually touched the part. Any idea why is this happening ?

This is the LocalScript :
Code:
local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")

local deb = Instance.new('BoolValue', ReplicatedStorage)
deb.Name = Player.Name..'_debounce'
deb.Value = true
local debounce = deb.Value
debounce = true

function getPlayerFromCharacter(character)
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player.Character == character then
            return player
        end
    end
end

workspace.touchPart.Touched : connect (function(part) 
    if debounce then
        local plr = getPlayerFromCharacter (part.Parent)
        print (tostring(plr)..'  has touched the red part')
        touchEvent:FireServer()
    end
    debounce = false
end)

local function printPlayerName(juc)
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = tostring(juc)
    Player.PlayerGui.TouchedGui.Enabled  = true
    wait (3)
    Player.PlayerGui.TouchedGui.Enabled  = false
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = ''
end

printEvent.OnClientEvent:Connect(printPlayerName)
And this is the ServerScript :

Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")

touchEvent.OnServerEvent:Connect(function(playa)
    print ('server has received this name : '..tostring(playa) ) -- this is where the server is returning all of the players names
    printEvent : FireAllClients(playa)                                        --instead of just the one player who has touched the part
end)
It seems like you have a lot going on for this, is there a reason you don't run the event on the server? Also, if the client is fired while the function is already running, it could produce an undesirable result. I'd probably opt for something more like this.

Server:
Code:
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TouchEvent = ReplicatedStorage:WaitForChild('TouchEvent')
local TouchPart = workspace:WaitForChild('TouchPart')
local Debounce = {}

TouchPart.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then
        print(tostring(Player) .. ' is not a player.')
        return
    end
    if Debounce[tostring(Player)] then
        print(tostring(Player) .. ' has already touched part.')
        return
    end
    Debounce[tostring(Player)] = true
    print(tostring(Player) .. ' has touched part.')
    TouchEvent:FireAllClients(Player)
end)
Client:
Code:
local Players = game:GetService('Players')
local TouchedGui = Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('TouchedGui')
TouchedGui.Enabled = false
local PlayerLabel = TouchedGui:WaitForChild('PlayerLabel')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TouchEvent = ReplicatedStorage:WaitForChild('TouchEvent')
local TouchPart = workspace:WaitForChild('TouchPart')
local TouchedQueue = {}

TouchEvent.OnClientEvent:Connect(function(Player)
    print(tostring(Player) .. ' received by client.')
    table.insert(TouchedQueue, Player)
    if TouchedGui.Enabled then
        print('Queue loop is already running.')
        return
    end
    repeat
        PlayerLabel.Text = tostring(TouchedQueue[1]) .. ' has done the thing.'
        TouchedGui.Enabled = true
        wait(3)
        table.remove(TouchedQueue, 1)
    until #TouchedQueue <= 0
    TouchedGui.Enabled = false
end)
 
Joined
Jun 4, 2018
Messages
240
Ratings
11
Roblox
proclet
-I have the "part" in workspace that they have to touch to win but no idea how to script that.
Where this is vital to the mechanics of your game, I'd recommend also checking magnitude. People claim that exploiters can send false positives to Touched events. It should look something like this, and could be easily added to your main script.
Code:
local Players = game:GetService('Players')

local Part = script.Parent
local MaxDistance = 15

Part.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then -- check if the touched part is a player
        print(tostring(Hit) .. ' does not belong to any character.')
        return
    end
    local RootPart = Hit.Parent:FindFirstChild('HumanoidRootPart')
    if (RootPart.Position - Part.Position).magnitude <= MaxDistance then
        print('Player is near part and has escaped.')
        -- move player to EscapedPlayers table
    else
        print('Player is not near part, and may be an exploiter.')
    end
end)
Other than that, I'd runs checks on remaining players whenever someone leaves, dies, or escapes. Track escaped players in a table, and remove them from it if they leave. If only the bloxxer remains, and no one escaped, give the bloxxer the win. If only the bloxxer remains but players escaped, give the players the win. If the bloxxer leaves, either restart the match or give players the win.
Hey CallMeKY, I'm trying to make this a bit more fancy, like adding some sort of Gui message instead of the plain "print". Unfortunately, the server is returning all of the players, instead of just the one who has actually touched the part. Any idea why is this happening ?

This is the LocalScript :
Code:
local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")

local deb = Instance.new('BoolValue', ReplicatedStorage)
deb.Name = Player.Name..'_debounce'
deb.Value = true
local debounce = deb.Value
debounce = true

function getPlayerFromCharacter(character)
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player.Character == character then
            return player
        end
    end
end

workspace.touchPart.Touched : connect (function(part)
    if debounce then
        local plr = getPlayerFromCharacter (part.Parent)
        print (tostring(plr)..'  has touched the red part')
        touchEvent:FireServer()
    end
    debounce = false
end)

local function printPlayerName(juc)
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = tostring(juc)
    Player.PlayerGui.TouchedGui.Enabled  = true
    wait (3)
    Player.PlayerGui.TouchedGui.Enabled  = false
    Player.PlayerGui.TouchedGui.PlayerLabel.Text = ''
end

printEvent.OnClientEvent:Connect(printPlayerName)
And this is the ServerScript :

Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local touchEvent = ReplicatedStorage:WaitForChild("TouchEvent")
local printEvent = ReplicatedStorage:WaitForChild("PrintEvent")

touchEvent.OnServerEvent:Connect(function(playa)
    print ('server has received this name : '..tostring(playa) ) -- this is where the server is returning all of the players names
    printEvent : FireAllClients(playa)                                        --instead of just the one player who has touched the part
end)
It seems like you have a lot going on for this, is there a reason you don't run the event on the server? Also, if the client is fired while the function is already running, it could produce an undesirable result. I'd probably opt for something more like this.

Server:
Code:
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TouchEvent = ReplicatedStorage:WaitForChild('TouchEvent')
local TouchPart = workspace:WaitForChild('TouchPart')
local Debounce = {}

TouchPart.Touched:Connect(function(Hit)
    local Player = Players:GetPlayerFromCharacter(Hit.Parent)
    if not Player then
        print(tostring(Player) .. ' is not a player.')
        return
    end
    if Debounce[tostring(Player)] then
        print(tostring(Player) .. ' has already touched part.')
        return
    end
    Debounce[tostring(Player)] = true
    print(tostring(Player) .. ' has touched part.')
    TouchEvent:FireAllClients(Player)
end)
Client:
Code:
local Players = game:GetService('Players')
local TouchedGui = Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('TouchedGui')
TouchedGui.Enabled = false
local PlayerLabel = TouchedGui:WaitForChild('PlayerLabel')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TouchEvent = ReplicatedStorage:WaitForChild('TouchEvent')
local TouchPart = workspace:WaitForChild('TouchPart')
local TouchedQueue = {}

TouchEvent.OnClientEvent:Connect(function(Player)
    print(tostring(Player) .. ' received by client.')
    table.insert(TouchedQueue, Player)
    if TouchedGui.Enabled then
        print('Queue loop is already running.')
        return
    end
    repeat
        PlayerLabel.Text = tostring(TouchedQueue[1]) .. ' has done the thing.'
        TouchedGui.Enabled = true
        wait(3)
        table.remove(TouchedQueue, 1)
    until #TouchedQueue <= 0
    TouchedGui.Enabled = false
end)
Thank you, your code works great. I think I need to study more the RemoteEvents and Functions :P
Post automatically merged:

Question : which part of the character touching the workspace.TouchPart can trigger this line : print(tostring(Player) .. ' is not a player.') ?
 
Last edited:

CallMeKY

Active Member
Joined
Feb 25, 2018
Messages
387
Ratings
115
Roblox
CallMeKY
Discord
CallMeKY#5012
Question : which part of the character touching the workspace.TouchPart can trigger this line : print(tostring(Player) .. ' is not a player.') ?
This would catch any random parts that hit, like a bullet or an equipped tool.