Character Button Spawn

so im try to make it so that the player spawns in when it clicks the button, and im having difficulties doing it

i spend 4 hours trying to do this and it keeps spawning in automatically

(Button) Client Script:



local player = game.Players.LocalPlayer
local MainMenu = script.Parent:WaitForChild("MainMenu")
local ScreenGui = script.Parent:WaitForChild("ScreenGui")
local healthBar = script.Parent:WaitForChild("HealthbarGui")

local button = MainMenu:WaitForChild("TextButton")

local campart = workspace.CM.CameraMenuu:WaitForChild("CameraMenu")

local event = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")


local connection

local camera = workspace.Camera

connection = game:GetService("RunService").Heartbeat:Connect(function()
	
camera.CameraSubject = workspace.CM.CameraMenuu.CameraMenu
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = campart.CFrame * CFrame.Angles(0,0,0)
	end)



button.MouseButton1Click:Connect(function()
	
		
		
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = player.Character.Humanoid
	
	MainMenu.Enabled = false
	ScreenGui.Enabled = true
	healthBar.Enabled = true
	

	
	connection:Disconnect()
	event:FireServer("Loaded")
end)

(CharacterSpawn) Server Script: this is a mess rn

local Players = game:GetService("Players")


Players:LoadCharacter()

task.wait(0.2)

Players.CharacterAutoLoads = false

local character = Players.LocalPlayer


local event = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")

local function add()

event.OnServerEvent:Connect(function(player, action)
	if action == "Loaded" then
		character:LoadCharacter()
	end
end)
end

character.CharacterAdded:Connect(add)

character:LoadCharacter()
1 Like

The simplest solution for you would be to set the parent of character to nil and then return it to the workspace.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

character.Parent = nil			-- hide
character.Parent = workspace 	-- show

You should also keep in mind that by disabling autoload you also disable the UI from loading.

3 Likes

Hi, @Teir199

basically server-script is not working because it is logic uncorrectly written and it is server-script ?
You can’t get localplayer in server-side script (unless localscript)

-- This is how it is supposed to be and script should be server-script
local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
local event = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")
event.OnServerEvent:Connect(function(player, action)
	if player.Character~=nil and action == "Loaded" then
		character:LoadCharacter()
	end
end)

about the client-side you need to reparent GUIs to LocalPlayer.PlayerGui otherwise GUIs won’t show (due to not appearing in LocalPlayer.PlayerGui).

God bless you :)

5 Likes

first in the server side being that you just used a method that didn’t exist like Players:LoadCharacter() cuz that is only available to actual Player class, not from the Players service [spoiler]you also used it on character which also does not exist cuz that’s a model[/spoiler]

second being character defined, LocalPlayer is only accessible to local script and if you use this on server side, it’ll return nothing as in code being nil, you’ll need to get it via any events that return the actual player class

and lastly third, character param is typed as Model which in roblox’s logic they dont exist, the event you’re getting is from Player class only, you also have a function that already connects an event with 2 args but only one is unused

im gonna align it properly here incase you’re still confused

local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
--you do not need a task.wait() func 

--removed `Players:LoadCharacter()` and `character:LoadCharacter()` 

local event = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")

--function removed cuz it's not that necessary
event.OnServerEvent:Connect(function(player, action)
	if action == "Loaded" then
		player:LoadCharacter() --use player arg, not character
	end
end)

as @leterence mentioned earlier, disabling this also kills the ability to let the game clone and run UI which wouldn’t show up on the first start, you would need to load them manually with script by storing the UI in ReplicatedFirst or ReplicatedStorage as those 2 tend to load first so that allows it to run on first start (almost) instantly

2 Likes

its very late imma read these tommorow

what i do for this kind of thing is use a server script with
Players.CharacterAutoLoads = false
to disable any future spawns if you need the player to go back into the menu (ui will already be loaded) then the first time you just load the character and destroy it.
for your button ui, ui a remote event to ask the server to spawn

--client
local UI = uipath
local SpawnButton:GuiButton = UI.button
local SpawnEvent = game.ReplicatedStorage:WaitForChild("SpawnEvent")

SpawnButton.MouseButton1Click:Connect(function()
	UI.Enabled = false
	SpawnEvent:FireServer()
end)
--make player be in main menu after death if you want
local plr = game.Players.LocalPlayer
local uiloaded = false
plr:GetPropertyChangedSignal("Character"):Connect(function()
	if not uiloaded then uiloaded = true return end
	if plr.Character == nil then
		UI.Enabled = true
	end
end)

--server

--let gui load
local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
Players.PlayerAdded:Connect(function(plr)
	plr:LoadCharacter()
	plr.CharacterAdded:Once(function()
		plr.Character = nil
	end)
end)

--handle spawn requests
local SpawnCoolDowns ={}
SpawnEvent.OnServerEvent:Connect(function(plr)
	if SpawnCoolDowns[plr] or nil ~= plr.Character then return end
	
	plr:LoadCharacter()
	
	SpawnCoolDowns[plr] = true
	task.wait(.5)
	SpawnCoolDowns[plr] = nil
end)
1 Like

i would do this but there another problem, in the client i added this thing where the players camera is spinning when they enter the game (sorry if i didnt tell you guys this part @BIASXED @pankii_kust @Microlosofto @leterence So, in doing that it will probably fix the gui’s but not this. I’m thinking of just spawning the players in an area where they can’t do nothing and teleport them into the player area, but I’ll still room here in the forum for alternatives.

when player clicks on button

Just set camera to custom and not scriptable

K ill deal with that later‎, but that’s not my main issue

Why don’t you just change the camera’s POV and put the player in a box for the time they haven’t clicked spawn?

this is what im leaning towards like i stated