Problems with server scripts not being able to pass data properly

I want to fix an issue with a server script returning nil when trying to change something as a string.

A few screenshots of the code:


Several different things I’ve tried is doing in the server script is sending data through the Remote2 event; here are some of the things I’ve tried:
game.ReplicatedStorage.TextLabel.Text = plr.Name
game.ReplicatedStorage.TextLabel.Text = tostring(plr.Name)
game.ReplicatedStorage.TextLabel.Text = tostringe(plr)
game.ReplicatedStorage.TextLabel.Text = plr
Also the plr variable comes from a local script running FireServer, it prints fine within the server script but doesn’t print properly in other scripts, and it doesn’t update any textlabel properly. Also ignore the "Value is not a valid member of Part “ServerScriptService.Generators.Generator + 5/s” as that’s not part of the problem.

1 Like

Are you able to paste the code in a preformatted text?

Sure:
ServerScript:

local Remote2 = game.ReplicatedStorage.Remotes:WaitForChild("BlockVal")
local Remote3 = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")
local ReplicatedStorage = game.ReplicatedStorage
local Blocks = ReplicatedStorage.Blocks
local CloneName = "Wood"
local Run = 0
local Names = script.Parent.Names
local PreNameCheck = "?ewrwer432r34r3r34r34r43r34r34r"
local TableFP = {

}
--[[Remote3.OnServerEvent:Connect(function(plr, CN, RunSwitch) task.wait()
	if plr.Name ~= PreNameCheck then

	else
		if RunSwitch == nil then

		else
			Run = RunSwitch
		end
		CloneName = CN
	end
end)]]
local SurfaceVectors = {
	Top = Vector3.new(0, 1, 0),
	Bottom = Vector3.new(0, -1, 0),
	Left = Vector3.new(-1, 0, 0),
	Right = Vector3.new(1, 0, 0),
	Front = Vector3.new(0, 0, -1),
	Back = Vector3.new(0, 0, 1),
}
Remote.OnServerEvent:Connect(function(plr, Pos, MP) task.wait()
	game.ReplicatedStorage.TextLabel.Text = plr.Name -- this wont work, also I've put this in a variety of positions and rewrote it several times 
	if Names.Name1.Text == "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w" and Names.Name2.Text ~= plr.Name and Names.Name3.Text ~= plr.Name and Names.Name4.Text ~= plr.Name and Names.Name5.Text ~= plr.Name and Names.Name6.Text ~= plr.Name and Names.Name7.Text ~= plr.Name and Names.Name8.Text ~= plr.Name and Names.Name9.Text ~= plr.Name and Names.Name10.Text ~= plr.Name and Names.Name11.Text ~= plr.Name and Names.Name12.Text ~= plr.Name and Names.Name13.Text ~= plr.Name and Names.Name14.Text ~= plr.Name and Names.Name15.Text ~= plr.Name then
		Names.Name1.Text = plr.Name
		PreNameCheck = plr.Name
	elseif game.Players:FindFirstChild(plr.Name) == nil then
		Names.Name1.Text = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
		PreNameCheck = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
		game.ReplicatedFirst.Names.Name1.Text = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
		local CloneName = "Wood"
	end
	if Names.Name1.Text ~= "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w" and game.Players:FindFirstChild(plr.Name) ~= nil then
		Remote2:FireClient(plr)
		Remote3.OnServerEvent:Connect(function(plr, CN, RunSwitch) task.wait()
			if plr.Name ~= PreNameCheck then

			elseif plr.Name == PreNameCheck then
				if RunSwitch == nil then

				else
					Run = RunSwitch
				end
				CloneName = CN
			end
		end)
		if Run == 1 then
			local Cost = 15
			if CloneName == "Bomb" then	Cost = 250 elseif CloneName == "Wood" then Cost = 15 elseif CloneName == "Stone" then Cost = 55 elseif CloneName == "Lava" then Cost = 350 elseif CloneName == "Healer" then Cost = 2500 elseif CloneName == "LightBulb" then Cost = 175 end
			if plr.leaderstats.Cash.Value >= Cost then
				local Clone = game.ReplicatedStorage.Blocks:FindFirstChild(CloneName):Clone()
				local surface = MP
				local surfaceVector = SurfaceVectors[surface]
				local adjustedX = Pos.X + surfaceVector.X
				local adjustedY = Pos.Y + surfaceVector.Y
				local adjustedZ = Pos.Z + surfaceVector.Z
				Clone.Parent = game.Workspace.Block
				Clone.Player.Text = plr.Name
				Clone.Position = Vector3.new(math.round(adjustedX / 4) * 4, math.round(adjustedY/ 4) * 4+2, math.round(adjustedZ / 4) * 4)
				if Clone.Name == "Bomb" or Clone.Name == "Lava" or Clone.Name == "Healer" then
					Clone.Part.Position = Vector3.new(math.round(adjustedX / 4) * 4, math.round(adjustedY/ 4) * 4+3.5, math.round(adjustedZ / 4) * 4)
				end
				local CloneGTP = Clone:GetTouchingParts()
				if #CloneGTP > 0 then
					for i = 1, #CloneGTP do
						if CloneGTP ~= "Baseplate" then
							Clone:Destroy()
						end
					end
					plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + Cost
				end
				plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - Cost
			else

			end
		end
	end
end)```
Local Script:
```repeat wait() until game.ReplicatedStorage:WaitForChild("Remotes")
local BlockVal = game.ReplicatedStorage.Remotes:WaitForChild("BlockVal")
local BlockType = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")
BlockVal.OnClientEvent:Connect(function(plr)
	local NameCheck = script.Parent.Names.Name1.Text
	local Player = game.Players.LocalPlayer
	print(tostring(plr).." :NameCheck; Player: ") -- this print is to test if it's not nil
	if Player.Name ~= NameCheck then

	elseif Player.Name == NameCheck then
		local BV = game.ReplicatedStorage.BlockVal
		wait(0.1)
		local SN = "Wood"
		if BV.Value == 0 then
			SN = "Bomb"
		elseif BV.Value == 1 then
			SN = "Wood"
		elseif BV.Value == 2 then
			SN = "Stone"
		elseif BV.Value == 3 then
			SN = "Lava"
		elseif BV.Value == 4 then
			SN = "Healer"
		elseif BV.Value == 5 then
			SN = "LightBulb"
		end
		local SV = game.ReplicatedStorage.SwitchVal
		BlockType:FireServer(SN, SV.Value)
	end
end)```
Another Local Script:
```local PlaceRe = game.ReplicatedStorage.Remotes:WaitForChild("PlaceRe")
local BlockType = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")
local Button = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local MousePos = 0
local function Clicked()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local MP = mouse.TargetSurface.Name
	PlaceRe:FireServer(mouse.Hit.Position, MP)
end
mouse.Button1Down:Connect(Clicked)

This should fix it
what it does is Checks for nil values: Ensure plr and plr.Name are not nil before using them.

Server Script

local Remote2 = game.ReplicatedStorage.Remotes:WaitForChild("BlockVal")
local Remote3 = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")
local ReplicatedStorage = game.ReplicatedStorage
local Blocks = ReplicatedStorage.Blocks
local CloneName = "Wood"
local Run = 0
local Names = script.Parent.Names
local PreNameCheck = "?ewrwer432r34r3r34r34r43r34r34r"
local TableFP = {}

local SurfaceVectors = {
    Top = Vector3.new(0, 1, 0),
    Bottom = Vector3.new(0, -1, 0),
    Left = Vector3.new(-1, 0, 0),
    Right = Vector3.new(1, 0, 0),
    Front = Vector3.new(0, 0, -1),
    Back = Vector3.new(0, 0, 1),
}

Remote.OnServerEvent:Connect(function(plr, Pos, MP)
    task.wait()
    if plr and plr.Name then
        print("Player name received: " .. plr.Name)
        local textLabel = game.ReplicatedStorage:FindFirstChild("TextLabel")
        if textLabel then
            textLabel.Text = plr.Name
            print("TextLabel updated to: " .. plr.Name)
        else
            print("TextLabel not found in ReplicatedStorage.")
        end
    else
        print("Invalid player object or player name is nil.")
    end

    if Names.Name1.Text == "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w" and Names.Name2.Text ~= plr.Name and Names.Name3.Text ~= plr.Name and Names.Name4.Text ~= plr.Name and Names.Name5.Text ~= plr.Name and Names.Name6.Text ~= plr.Name and Names.Name7.Text ~= plr.Name and Names.Name8.Text ~= plr.Name and Names.Name9.Text ~= plr.Name and Names.Name10.Text ~= plr.Name and Names.Name11.Text ~= plr.Name and Names.Name12.Text ~= plr.Name and Names.Name13.Text ~= plr.Name and Names.Name14.Text ~= plr.Name and Names.Name15.Text ~= plr.Name then
        Names.Name1.Text = plr.Name
        PreNameCheck = plr.Name
    elseif game.Players:FindFirstChild(plr.Name) == nil then
        Names.Name1.Text = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
        PreNameCheck = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
        game.ReplicatedFirst.Names.Name1.Text = "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w"
        CloneName = "Wood"
    end

    if Names.Name1.Text ~= "sa0d43nofdbvb59vbbs[bfp3w[rbf5rw34bg9uv954w" and game.Players:FindFirstChild(plr.Name) ~= nil then
        Remote2:FireClient(plr)
        Remote3.OnServerEvent:Connect(function(plr, CN, RunSwitch)
            task.wait()
            if plr.Name ~= PreNameCheck then
                -- Do nothing
            elseif plr.Name == PreNameCheck then
                if RunSwitch == nil then
                    -- Do nothing
                else
                    Run = RunSwitch
                end
                CloneName = CN
            end
        end)
        if Run == 1 then
            local Cost = 15
            if CloneName == "Bomb" then
                Cost = 250
            elseif CloneName == "Wood" then
                Cost = 15
            elseif CloneName == "Stone" then
                Cost = 55
            elseif CloneName == "Lava" then
                Cost = 350
            elseif CloneName == "Healer" then
                Cost = 2500
            elseif CloneName == "LightBulb" then
                Cost = 175
            end
            if plr.leaderstats.Cash.Value >= Cost then
                local Clone = game.ReplicatedStorage.Blocks:FindFirstChild(CloneName):Clone()
                local surface = MP
                local surfaceVector = SurfaceVectors[surface]
                local adjustedX = Pos.X + surfaceVector.X
                local adjustedY = Pos.Y + surfaceVector.Y
                local adjustedZ = Pos.Z + surfaceVector.Z
                Clone.Parent = game.Workspace.Block
                Clone.Player.Text = plr.Name
                Clone.Position = Vector3.new(math.round(adjustedX / 4) * 4, math.round(adjustedY / 4) * 4 + 2, math.round(adjustedZ / 4) * 4)
                if Clone.Name == "Bomb" or Clone.Name == "Lava" or Clone.Name == "Healer" then
                    Clone.Part.Position = Vector3.new(math.round(adjustedX / 4) * 4, math.round(adjustedY / 4) * 4 + 3.5, math.round(adjustedZ / 4) * 4)
                end
                local CloneGTP = Clone:GetTouchingParts()
                if #CloneGTP > 0 then
                    for i = 1, #CloneGTP do
                        if CloneGTP[i].Name ~= "Baseplate" then
                            Clone:Destroy()
                        end
                    end
                end
                plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - Cost
            end
        end
    end
end)

Put in StarterPlayerScripts Local script

repeat wait() until game.ReplicatedStorage:WaitForChild("Remotes")
local BlockVal = game.ReplicatedStorage.Remotes:WaitForChild("BlockVal")
local BlockType = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")

BlockVal.OnClientEvent:Connect(function(plr)
    local NameCheck = script.Parent.Names.Name1.Text
    local Player = game.Players.LocalPlayer
    print(tostring(plr).." :NameCheck; Player: ") -- this print is to test if it's not nil
    if Player.Name ~= NameCheck then
        -- Do nothing
    elseif Player.Name == NameCheck then
        local BV = game.ReplicatedStorage.BlockVal
        wait(0.1)
        local SN = "Wood"
        if BV.Value == 0 then
            SN = "Bomb"
        elseif BV.Value == 1 then
            SN = "Wood"
        elseif BV.Value == 2 then
            SN = "Stone"
        elseif BV.Value == 3 then
            SN = "Lava"
        elseif BV.Value == 4 then
            SN = "Healer"
        elseif BV.Value == 5 then
            SN = "LightBulb"
        end
        local SV = game.ReplicatedStorage.SwitchVal
        BlockType:FireServer(SN, SV.Value)
    end
end)

StarterPlayerScripts local script

local PlaceRe = game.ReplicatedStorage.Remotes:WaitForChild("PlaceRe")
local BlockType = game.ReplicatedStorage.Remotes:WaitForChild("BlockType")
local Button = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

local function Clicked()
    local mouse = game.Players.LocalPlayer:GetMouse()
    local MP = mouse.TargetSurface.Name
    PlaceRe:FireServer(mouse.Hit.Position, MP)
end

mouse.Button1Down:Connect(Clicked)

Also, why are the Local scripts all in one code?
Its easier to manage if its modular.

1 Like

From what I can see, when you are using tostring. You are using it on the player instance. Which will return nil.

You can simply get the name of a player as a string by doing player.Name. And the type will be a string.

I’ve tried that, it says, “Attempt to index nil with “name””

The local script still outputs, "nil :NameCheck; Player: " with the print even with the code adjustment, also what do you mean by all in one code?

That’s because there is no “player” parameter in the “onclientevent”.

You can simply get the player by putting at the top:

local plr = game.Players.LocalPlayer

The player instance isn’t a usable parameter on the event unlike the server, because you already are able to access the correct player instance.

Ok, the only reason why I tried to pass a player parameter through the remote event, was to check to see if the sending server script was also sending to the same local script assigned to a specific player; which is why I checked if the local player and the parameter of a player were both the same. So if you do Remote:FireClient(plr.Name) on the server, it wouldn’t acknowledge the player’s name on the client when doing Remote.OnCientEvent(plr)?

This worked, it seems that it was able to change the textlabel, which is really all I needed for it to work as intended.

1 Like

The FireClient method doesn’t automatically pass the player parameter to the onclientevent like it does with the server.

So the “plr” is basically nil.

You can read this on the documentation too if you wish.

The reason roblox doesn’t automatically pass it is because there is simply no need, since you can get the player instance simply through LocalPlayer.

Also for your BlockVal.OnClientEvent code, the whole if/elseif statements will become a real mess if you need like 40 or so values, i will suggest to use something like this.

BlockVal.OnClientEvent:Connect(function(plr)
    local NameCheck = script.Parent.Names.Name1.Text
    local Player = game.Players.LocalPlayer
    print(tostring(plr).." :NameCheck; Player: ") -- this print is to test if it's not nil

    if Player.Name == NameCheck then
        local BV = game.ReplicatedStorage.BlockVal
        task.wait(0.1)

        local blockTypes = {
            [0] = "Bomb",
            [1] = "Wood",
            [2] = "Stone",
            [3] = "Lava",
            [4] = "Healer",
            [5] = "LightBulb"
        }

        -- Default to "Wood" if BV.Value is not in the table
        local SN = blockTypes[BV.Value] or "Bomb"

        local SV = game.ReplicatedStorage.SwitchVal
        BlockType:FireServer(SN, SV.Value)
    end
end)