Why doesn't this script (in ServerScriptService) accept and connect to an event sent by a LocalScript (in StarterPlayerScripts)

So, in the bottom of this server-sided code, you will notice I am listening for an event sent by client to let the script know to change player’s class within the player profiles tables I’ve created.

local ServerStorage = game:GetService("ServerStorage")
local WeaponsFolder = ServerStorage:WaitForChild("Weapons")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local PlayerList = {}

local classEvent = ReplicatedStorage:WaitForChild("classEvent")
local workersAxe = WeaponsFolder:WaitForChild("Worker's Axe") 

local function OnPlayerAdded(player)
	print("Player added: " .. player.Name)
	local playerData = {
		player = player,
		character = nil,
		class = "Worker"
	}
	table.insert(PlayerList, playerData)

	player.CharacterAdded:Connect(function(character)
		print("Character added for player: " .. player.Name)
		playerData.character = character
		local backpack = player:WaitForChild("Backpack")
		local clonedTool = workersAxe:Clone()
		clonedTool.Parent = backpack
	end)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

-- Function to find a player in the PlayerList
local function FindPlayerData(player)
	for _, playerData in ipairs(PlayerList) do
		if playerData.player == player then
			return playerData
		end
	end
	return nil
end

-- Function to change a player's class
local function ChangePlayerClass(player, newClass)
	local playerData = FindPlayerData(player)
	if playerData then
		playerData.class = newClass
		print(player.Name .. " changed class to " .. newClass)
	else
		warn("Player not found in PlayerList")
	end
end

local function PrintPlayerList()
	for _, playerData in ipairs(PlayerList) do
		print("Player: " .. playerData.player.Name .. ", Character: " .. tostring(playerData.character) .. ", Class: " .. playerData.class)
	end
end

-- [DEBUG] Print the player list every 10 seconds for debugging
while true do
	wait(10)
	PrintPlayerList()
end

----- Event Connectors -----

classEvent.OnServerEvent:Connect(function(plr, newClass)
	print("Event received from client: " .. plr.Name .. " requesting class change to " .. newClass)
	ChangePlayerClass(plr, newClass)
	plr.Character:BreakJoints()
end)

There is no issue with client script, it sends the event as it’s meant to, here it is as well.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local player = Players.LocalPlayer
local classEvent = ReplicatedStorage:WaitForChild("classEvent")

local centerPart = Workspace:WaitForChild("classSelector")
local prompt = centerPart:FindFirstChildWhichIsA("ProximityPrompt")
local center = centerPart.Position

if prompt then
	print("Prompt found")
else
	warn("ProximityPrompt not found")
end

prompt.Triggered:Connect(function(playerWhoTriggered)
	if playerWhoTriggered == player then
		print("Prompt triggered by local player")
		classEvent:FireServer("Knight")
	else
		print("Prompt triggered by another player")
	end
end)

So why wouldn’t these two scripts work with together? Thanks in advance

what output are you getting with those prints?

this is the problem, wrap it in a new thread instead with

task.defer(function()
   while true do
	  task.wait(10)
      PrintPlayerList()
   end
end)
1 Like

Than you so much, this worked perfectly, I expected event connectors to work in any place inside of the script. Is that not the case?

the problem is that it your while loop will keep running which causes the rest of the script to never run

why’d you mark your own post as a solution lol?

I didn’t notice lol, my bad :joy: Thanks for helping out

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.