What is wrong with this script?

Hello I was working on my project I wanted to make a button that when a player clicks on it the player’s character parent will be on a folder but nope I even used prints but still not what is wrong with it

its a script inside a textbutton

local joinmanager = require(game.ServerScriptService:WaitForChild("JoinManager"))

function click()
	print("clicked")
	if #game.Workspace.Level1Folder:GetChildren() <= 5 then
		print("can join")
		joinmanager:AddPlayerToLevel1(script.Parent.Parent.Parent.Parent.Parent)
	end
end

script.Parent.MouseButton1Up:Connect(click)
script.Parent.TouchTap:Connect(click)

Does it print “can join”?

Also please post the contents of the module also.

Are you running this from a server or local script? Where is the button? Is it GUI?

nope it prints nothing here is the module script

local module = {}
local level1 = {}

function module:AddPlayerToLevel1(player)
	if game.Players:FindFirstChild(player.Name) then
		game.Players:FindFirstChild(player.Name).Character.Parent = game.Workspace
		game.Players:FindFirstChild(player.Name).Character.Parent = game.Workspace.Level1Folder
		print("joined")
	end
end

while true do
	wait(5)
	table.clear(level1)
	for i,v in ipairs(game.Workspace.Level1Folder:GetChildren()) do
		table.insert(level1, v)
	end
	if #level1 >= 2 then
		game:GetService("TeleportService"):TeleportPartyAsync(6108619940, level1)
	end
	wait(0)
end 

return module

Ok so from what I can tell here you are trying to click a GUI button and have the server move the player into a folder. Later you teleport all players into a game.

First of all, the button probably runs a local script. If you don’t have a local script for the button then that is the reason you don’t see the print.

Secondly, to make a local script communicate with the server you need a remote event or a remote function (here it is better to use a remote function). The problem that you have right now is that when you call a module from a local script, the module is also local. So you cannot move the player’s character into the desired folder (you can, but only the client will see it).

So let’s start easy:

First create a RemoteFunction object in game.ReplicatedStorage (call it “AddPlayerToLevel”). Then move the initial script into a local script (in the exact same location, just swap from normal script to local). Make the script this instead of what you had.

local AddPlayerToLevel = game.ReplicatedStorage:WaitForChild("AddPlayerToLevel")

function click()
	print("clicked")
	if AddPlayerToLevel:InvokeServer(1) then
        print("Player Added")
    else
        print("Player not added")
    end
end

script.Parent.Activated:Connect(click)

Now create another script in ServerScriptService:

local AddPlayerToLevel = game.ReplicatedStorage:WaitForChild("AddPlayerToLevel")
local joinmanager = require(game.ServerScriptService:WaitForChild("JoinManager"))

function onAddPlayerToLevel(player, level)
    if level == 1 then
        if #game.Workspace.Level1Folder:GetChildren() <= 5 then
	       print("can join")
	       joinmanager.AddPlayerToLevel1(player)
           return true
        else
           return false
        end
    end
    return false
end

AddPlayerToLevel.OnServerInvoke = onAddPlayerToLevel

Try this and let me know if it works. What we do here is use a remote function to tell the server we want a player added to level 1. The server will reply true/false to let the client know if it successfully added the player to the folder.

EDIT: I Noted another problem in your solution. You cannot have an infinite while in a module script. This causes the require to never finish.

You also need to modify the module:

local module = {}

function module.AddPlayerToLevel1(player)
	if game.Players:FindFirstChild(player.Name) then
		game.Players:FindFirstChild(player.Name).Character.Parent = game.Workspace.Level1Folder
		print("joined")
	end
end

return module

And create another script and put the while there:

local Players = game:GetService("Players")
local level1 = {}

while true do
	wait(5)
	table.clear(level1)
	for i,v in ipairs(game.Workspace.Level1Folder:GetChildren()) do
		table.insert(level1, Players:GetPlayerFromCharacter(v))
	end
	if #level1 >= 2 then
		game:GetService("TeleportService"):TeleportPartyAsync(6108619940, level1)
	end
	wait(0)
end 
1 Like

I edited my previous post a few times so make sure to get the latest changes. Ping if you encounter any issues.

1 Like