Hello, I have made a script that I wish to do the following:
There are 2 parts, when a player stands on ‘part1’ it turns blue and if a player stands on ‘part2’ it goes blue.
If there is a player on ‘part1’ AND ‘part2’ a playbutton gui appears on their screen.
If EITHER player clicks on the gui, they are both teleported to separate parts, lets say called ‘team1’ and ‘team2’
I have been trying to do this for ages and am very new to scripting so I am having lots of problems! I would be extremely grateful if anyone could help!
Here’s my script that is in server script:
local Players = game:GetService("Players")
local RemoteEvent = ReplicatedStorage:FindFirstChild("RemoteEvent")
local part1 = game.workspace:FindFirstChild("Player1")
local part2 = game.Workspace:FindFirstChild("Player2")
local standingOnPart1 = false
local standingOnPart2 = false
local playButton = game.StarterGui.ScreenGui.Frame
-- Call functions
local function startEvent()
end
-- Remote Events
RemoteEvent.OnServerEvent:Connect(function(player)
-- When fired execute
if standingOnPart1 == true and standingOnPart2 == true then -- Condition
playButton.Visible = true
end
end)
-- 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 CharacterFromPart = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if CharacterFromPart then -- Check if it found character
if standingOnPart1 ~= true and part1.Name ~= CharacterFromPart.Name then -- Check if not standing and if not occupied
part1.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart1 = true
part1.Name = CharacterFromPart.Name
startEvent() -- Call event
end
end
end)
part1.TouchEnded:Connect(function(TouchedPart)
local CharacterFromPart = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if CharacterFromPart then -- Check if it found character
if standingOnPart1 == true and part1.Name == CharacterFromPart.Name then -- Check if standing and is occupied
part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart1 = false
part1.Name = "part1"
startEvent() -- Call event
end
end
end)
-----------------------------------------------------
-- PART2 Events --
-----------------------------------------------------
part2.Touched:Connect(function(TouchedPart) -- Touched
local CharacterFromPart = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if CharacterFromPart then -- Check if it found character
if standingOnPart2 ~= true and part2.Name ~= CharacterFromPart.Name then -- Check if not standing and if not occupied
part2.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart2 = true
part2.Name = CharacterFromPart.Name
startEvent() -- Call event
end
end
end)
part2.TouchEnded:Connect(function(TouchedPart)
local CharacterFromPart = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if CharacterFromPart then -- Check if it found character
if standingOnPart2 == true and part2.Name == CharacterFromPart.Name then -- Check if standing and is occupied
part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart2 = false
part2.Name = "part2"
startEvent() -- Call event
end
end
end)
I think the issue with your code is that you haven’t fired the remote event and that the variable “remote event”, you forgot to mention game.ReplicatedStorage. Also, do not use StarterGui to get the player’s screengui to open up, because it will not work. Instead, you should use PlayerGui.
Instead of constantly changing the part’s name, I think you should change the standOnPart1/2 variables as the player or nil.
What I will do is:
Insert 4 parts in workspace (Part1, Part2, team2, team1).
Add a remote event in replicatedstorage.
Then, for the server-sided script I changed your original code a bit so it looks like:
local Players = game:GetService("Players")
local part1 = workspace.Part1
local part2 = workspace.Part2
local standingOnPart1 = nil
local standingOnPart2 = nil
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local function startEvent()
print("START")
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[standingOnPart2.Name]:MoveTo(workspace.team2.Position)
workspace[standingOnPart1.Name]:MoveTo(workspace.team1.Position)
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
Finally, the local script in the playbutton to fire the remote event, do:
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
script.Parent.MouseButton1Click:Connect(function()
RemoteEvent:FireServer() -- fires event
end)
When a player joins/loads up in a server, a player has instances within such as: Backpack, PlayerGui etc.
StarterGui is basically a placeholder for all the main screenguis that will be given to the player’s PlayerGui when they join. Thus, each player has a copy of the screenguis that is inside the StarterGui.
Basically, startergui acts similarly like starterpack. Like how tools in starterpack will be given to player’s backpack.
The Gui still isnt showing up for some reason… have I put the script in the right place and did I need to name any of my scripts anything in perticular?
Oh… well could you test it in studio with 2 players locally and see the output? Also, did you made sure that you publish before testing it in the actual game?
I can’t test with my friend just yet, but i went in by myself and got this error message before I even touched the parts:
Players.rafferty05.PlayerGui.ScreenGui.Frame.PlayButtonScript:3: attempt to index nil with ‘LocalPlayer’
Well the local script code I gave did not mention anything with localplayer… Did you put the local script code in PlayButtonScript or the localscript inside the playbutton?