Problem with RemoteFunction not sending name properly

  1. What do you want to achieve?
    I want to send Player Name from Text Box through Remote Function.

  2. What is the issue?
    ServerScriptService.petgivescript:4: attempt to concatenate string with nil

  3. What solutions have you tried so far?
    I tried to find solution here on DevForum but I didnt come with a solution.

Local Script inside StarterGui:

local Button = script.Parent:WaitForChild("Button")
local AcceptFrame = script.Parent.Parent.Parent.Parent.Parent.AcceptFrame
local PetChosen = script.Parent.Parent.Parent.Parent.Parent.PetSelector.PetSelectorFramer.PetSelectorBox
local PlayerName = script.Parent.Parent.Parent.Parent.Parent.PlayerNameSelectorX.PlayerNameFramer.PlayerNameBox

Button.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Folder.RemoteClone:InvokeServer(PetChosen.Text, PlayerName.Text)
	game.ReplicatedStorage.Folder.RemoteID:InvokeServer(PlayerName.Text)
	AcceptFrame.Visible = false
	print("invoked")
end)

Script inside ServerScriptService:

local RS = game.ReplicatedStorage

function RandomID(Player, PlayerName)
	print("1 "..PlayerName)
	local Rand = math.random(2,1000000)
	print("2 "..PlayerName)
	for i,v in pairs (game.Players:FindFirstChild(PlayerName).Pets:GetChildren()) do
		print("3 "..PlayerName)
		if v.PetID.Value == Rand then
			print("4 "..PlayerName)
			return RandomID()
			end
		end
	return Rand
end

RS.Folder.RemoteID.OnServerInvoke = RandomID

local function getpetrandom(Player, PetChosen, PlayerName)
	print(PetChosen.."1")
	local Settings = RS.Pets.Models:FindFirstChild(PetChosen).Settings
	if 1+1 == 2 then
		print(PetChosen.."2")
			local Clone = RS.Pets.PetFolderTemplate:Clone()
			Clone.PetID.Value = RandomID(PlayerName)
			Clone.Multiplier1.Value = Settings.Multiplier1.Value
			Clone.Multiplier2.Value = Settings.Multiplier2.Value
			print(PetChosen.."3")
			Clone.Type.Value = "Normal"
			Clone.Parent = PlayerName.Pets
			print(PetChosen.."5")
			Clone.Name = PetChosen
	end
	return PetChosen
end

RS.Folder.RemoteClone.OnServerInvoke = getpetrandom

Thanks for any solution Ive been trying to fix this for while now :).

1 Like

How are you getting PetChosen and both PlayerNames?

[Edit] Shouldn’t be like: OnserverInvoke = FunctionName(Args)

I am getting PetChosen and both PlayerNames from the Local Script inside StarterGui as you can see.
PetChosen and both player names are Texts and I am trying to invoke server with them.

Yes, but how is your server script getting these arguments?

I am getting them from OnServerInvokes?
Sorry I am bit confused now.

Also the “PetChosen” Text is printing but PlayerNames no.

My mistake, I was thinking completely differently. I honestly have no clue why this wouldn’t work. Hopefully a more advanced developer can solve your issue.

Dont worry thank you for trying to help with my problem :).

Hello!

You’ve used RS.Folder.RemoteID.OnServerInvoke = RandomID.
While this will fire when the server is invoked, you are not passing the player name as an argument through the RandomID function, therefore when the function looks for PlayerName it will come up with nil.

use RS.Folder.RemoteID.OnServerInvoke = RandomID(Player,PlayerName) instead.

This should help.

:star:

I believe you are incorrect. He was doing it correctly. Putting the parenthesis with it like how you did it will invoke the function once the script runs and make an error. It should be tested anyways but just giving my insight.

Can you paste which line is erroring? I can’t tell which one is line 4.

randomidproblem

print("1 "…PlayerName) This one basically every PlayerName is nil

Have you tried:

RS.Folder.RemoteID.OnServerInvoke = function(Player,PlayerName)
     RandomID(Player,PlayerName)
end)

I’ve written it out in a script myself and it’s not giving the “Unknown global” underlining error.

1 Like

You should try to print the PlayerName in the local script and see if you are getting anything there. It is likely that the PlayerName you are passing itself is nil, which makes it so that the server script is receiving nil as well.

Why would he do this? It’s redundant to do that when you can just write it in a single line like he did. It will still pass the arguments correctly as long as they’re defined in the function itself.

Yea its printing my name in the local script.
20:32:48.566 caviarbro - Client - LocalScript:7

No, that’s calling the function with null arguments.

When the function is automatically called, it passes the arguments as seen in the documentation
The issue here is most likely a replication error between the client and server

@caviarbro try sending a test string like:

game.ReplicatedStorage.Folder.RemoteID:InvokeServer("Test String")

and see if it still says it’s nil

Yea it is still saying nil.
game.ReplicatedStorage.Folder.RemoteID:InvokeServer(“String for test”)
Error:
ServerScriptService.petgivescript:4: attempt to concatenate string with nil

I’ve spotted the issue I think…

the getpetrandom function calls the RandomID function without the Player argument.

Change it to:

Clone.PetID.Value = RandomID(Player, PlayerName)