Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

So Im trying to make a Button which lets you spawn but it is doesnt work. What should I do?

I am making a FFA/TDM game where players can enter the game by pressing a button. The button is inside a ScreenGui which is inside the StarterGui. The script inside the button is a localscript

Code (LocalScript):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild( "Spawn")
script.Parent.MouseButton1Click:Connect(function()
    local Spawn = remfunction:InvokeServer()
	print("You have been teleported")
end)

ServerCode (On recieving command)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild("Spawn")
local function Deploy (player)
	print (player.Name.."wants to deploy")
	local status = game.ServerStorage.GameState
	print("variables defined")
	local character = player.Character
	status:GetPropertyChangedSignal("Value"):Connect(function()
		if status.Value == "1" then
			local ServerStorage = game:GetService("ServerStorage")
			local map = game:GetService("ServerStorage").CurrentMap.Value
			local spawnpoints = map:FindFirstChild("SpawnPoints")
			local AvailableSpawnPoints = spawnpoints:GetChildren()
			print("Teleporting function started")
			character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[math.random(1,#AvailableSpawnPoints)].CFrame + Vector3.new(0,2,0)
			wait(2)
			print("teleported to spawn")
				
			--Give weapon
			local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
			if equipped.Value ~= "" then
				local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
				weapon.Parent = player.Backpack
			else
				local Sword = ServerStorage.Sword:Clone()
				Sword.Parent = player.Backpack
			end
			print("weapon given")
			local GameTag = Instance.new("BoolValue")
			GameTag.Name = "GameTag"
			GameTag.Parent = character
			print("tag given")
			for _, plr in pairs(player) do
    			if not plr.Team then
        			local low = 9999
        			local lowTeam
        			for _, team in pairs(game:GetService('Teams'):GetChildren()) do
             			if #team:GetPlayers()<low then
                 			low = #team:GetPlayers()
                 			lowTeam = team
             			end
	       	 		end
        			plr.Team = lowTeam
   				end
			end
		print("placed in a team")
		print("good to go,start playing!")
	else
		print("you are in lobby stay there until the timer reaches zero :D")
		end
	end)
end

remfunction.OnServerInvoke = Deploy()

Output: ServerScriptService.SpawnScript:4: attempt to index local 'player' (a nil value)

Note: The value of GameState changes to 1 every second until the timer ends

The value of GameState switches to 0 after the timer ends

Server Code:

for i = Gamelength,0,-1 do
			game.ServerStorage.GameState.Value = "1"
			for x, player in pairs(plrs) do
					--Code I would not like to show Its similar to the previous server script
					else
						if character:FindFirstChild("GameTag") then
							--They are still alive
							print(player.Name.." is still in the game!")
						else
							--They are dead
							-- Code I would not like to show Its similar to the previous server script
						end
						print("teleported")
						end
					end
				else
					table.remove(plrs,x)
					print(player.Name.." has been removed!")
				end
			end
			
			Status.Value = "There are "..i.." seconds remaining"
			
			if i == 0 then
				RewardBestTDM(Reward)
				print(Status.Value)
				break
				end
			wait(1)
			end
	end	

	-- Cleanup Code
		Status.Value = "Game ended!"
		wait(5)
		game.Workspace.GameState.Value = "0"

Please help me!

4 comments

0

Does it teleport when the timer is 1? You put the if status.Value == "1" then in the localscript, I am not sure which way are you trying to do it, they teleport if the timer is running, or they teleport when the timer ended.

0

It is supposed to teleport when the timer is running

0

I just found an error at line 6, Can I make this server sided?

0

Local scripts cannot access ServerStorage or ServerScriptService

1 answer

✓ Accepted answer

Local scripts cannot be accessed through a serverstorage or serverscriptservice. It can only work if you add a remote event through a serverscript to local scripts.n

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunction = ReplicatedStorage:WaitForChild( "Spawn")
script.Parent.MouseButton1Click:Connect(function()
    local Spawn = remfunction:InvokeServer()
end)

14 comments

0

But,How to do that

0

ill accept the answer once i figure it out, thanks!

0

It didn't work, Ill update the question with the new scripts

0

Updated

0

Should I define the variables inside the function?

0

Yes

0

Still doesn't work i got an error

0

I didnt know what you were talking about please be more specific.

0

It doesnt teleport me when I press the button

0

Im using a RemoteFunction

0

What script can I help you on server or local

0

Give me a couple minutes to edit the code

0

I got another error

0

I solved the question yay, Thanks a lot for the remotefunction idea

This discussion is preserved as it was. New answers, comments, and votes are closed.