Hello, I am making a script so that when a player presses a ‘playButton’ gui, it teleports themselves and another player onto two separate parts. It then gives each player a sword and then I want to set a value for how many kills each player has got, so that I can make a statement like:
while ‘player1’ kills < 5 then
(set new spawn location)
(give sword)
end
Here is my script currently:
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local part1 = workspace.Part1
local part2 = workspace.Part2
local team1 = workspace.team1
local team2 = workspace.team2
local standingOnPart1 = nil
local standingOnPart2 = nil
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local function startEvent()
local SwordGiver = game.ServerStorage.Sword
local Sword1 = SwordGiver:Clone()
Sword1.Parent = standingOnPart1.Backpack
local char = standingOnPart1.Character
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:EquipTool(Sword1)
end
end
local Sword2 = SwordGiver:Clone()
Sword2.Parent = standingOnPart2.Backpack
local char = standingOnPart2.Character
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:EquipTool(Sword2)
end
end
end
RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
if standingOnPart2 ~= nil and standingOnPart1 ~= nil then
--Hide playbutton gui
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
--Teleports
workspace[standingOnPart1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
workspace[standingOnPart1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
workspace[standingOnPart2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
workspace[standingOnPart2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
local gameRunning = true
startEvent() -- triggers function
end
end)
-----------------------------------------------------
-- PART1 Events --
-----------------------------------------------------
-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]
part1.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart1 == nil then -- Check if not standing and if not occupied
part1.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart1 = Player
end
end
end)
part1.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart1 ~= nil then -- Check if standing and is occupied
if standingOnPart1.PlayerGui.ScreenGui.Enabled == true then
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
end
part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart1 = nil
if standingOnPart2 ~= nil then
if standingOnPart2.PlayerGui.ScreenGui.Enabled == true then
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
-----------------------------------------------------
-- PART2 Events --
-----------------------------------------------------
part2.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart2 == nil then -- Check if not standing and if not occupied
part2.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart2 = Player
end
end
end)
part2.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart2 ~= nil then -- Check if standing and is occupied
if standingOnPart2.PlayerGui.ScreenGui.Enabled == true then
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
end
part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart2 = nil
if standingOnPart1 ~= nil then
if standingOnPart1.PlayerGui.ScreenGui.Enabled == true then
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
while task.wait(0) do
if standingOnPart2 ~= nil and standingOnPart1 ~= nil then -- checks if both players are on top
standingOnPart2.PlayerGui.ScreenGui.Enabled = true
standingOnPart1.PlayerGui.ScreenGui.Enabled = true
end
end
Thank you very much to anyone able to give me some help!