RemoteEvent not working

Codes after “print” do not work.

RemoteEvent5.OnServerEvent:Connect(function(localplayer, id)
	print("yes test") -- printing this but after that it doesn't work
	local success, pizza_hm = pcall(function()
		return bank_data:GetAsync("cool_pizza_" ..id)
	end)
	if pizza_hm == "yes" then
		local status = "yes"
		RemoteEvent6:FireClient(id, status)
	else
		local status = 'no'
		RemoteEvent6:FireClient(id, status)
	end
end)
1 Like

not too sure here, but if the pcall has a success, the function gets terminated?

probably the return statement

1 Like

I think this is what you want, I think you were miss using pcalls.

RemoteEvent5.OnServerEvent:Connect(function(localplayer, id)
	print("yes test") -- printing this but after that it doesn't work
	local data = nil
	local success, failReason = pcall(function()
		data = bank_data:GetAsync("cool_pizza_" ..id)
	end)
	if success then
		if data == "yes" then
			local status = "yes"
			RemoteEvent6:FireClient(id, status)
		else
			local status = 'no'
			RemoteEvent6:FireClient(id, status)
		end
	else
		warn("DATASTORE ERROR: "..failReason)
	end
	
end)

pizza_hm was the data from the datastore right?

Oh yes i forgot but not solved.

attempt to concatenate string with Instance

Full code:

Script:

RemoteEvent5.OnServerEvent:Connect(function(localplayer, id_user)
	local data = nil
	local success, failReason = pcall(function()
		data = bank_data:GetAsync("cool_pizza_" ..id_user)
	end)
	if success then
		if data == "yes" then
			local status = "yes"
			RemoteEvent6:FireClient(id_user, status)
		else
			local status = 'no'
			RemoteEvent6:FireClient(id_user, status)
		end
	else
		warn("DATASTORE ERROR: "..failReason)
	end

end)

localscript:

local function onFocusLost(enterPressed, _inputObject)
	if enterPressed then
		local RemoteEvent4 = game.ReplicatedStorage.RemoteEvent4
		local RemoteEvent5 = game.ReplicatedStorage.RemoteEvent5
		local RemoteEvent6 = game.ReplicatedStorage.RemoteEvent6
		local id_user = Players.LocalPlayer.UserId
		local localplayer = Players.LocalPlayer
		local guess = textBox.Text
		if guess == 'i need cool pizza' then
			textBox.Text = "Wait, checking!"
			RemoteEvent5:FireServer(localplayer, id_user)
			RemoteEvent6.OnClientEvent:Connect(function(id_user, status)
				if status == "yes" then
					guess = "You already used this code."
					textBox.BackgroundColor3 = colorWrong
				else
					RemoteEvent4:FireServer(id_user)
					guess = "'Cool Pizza' added your bank account. You need rejoin"
					textBox.BackgroundColor3 = colorCorrect
				end
			end)
			RemoteEvent4:FireServer(id_user)
			textBox.BackgroundColor3 = colorCorrect
		else
			textBox.Text = "Invalid Code"
			textBox.BackgroundColor3 = colorWrong
		end
	else
		textBox.Text = ""
		textBox.BackgroundColor3 = colorNormal
	end
end
1 Like

Disregarding the interesting line-up of Remote Events you have there, this is where your problem is.

While firing events to the server, the Client/Player (firing the event) is automatically assigned as the first argument to the OnServerEvent(player, ...) function as a parameter, hence you do not need to pass the player as an argument.

RemoteEvent5:FireServer(id_user) should be a viable tentative fix to your current issue.

3 Likes

Why i getting Unable to cast value to Object error?

RemoteEvent5.OnServerEvent:Connect(function(player, localplayer, id_user)
	local data = nil
	local success, failReason = pcall(function()
		data = bank_data:GetAsync("cool_pizza_" ..id_user)
	end)
	if success then
		if data == "yes" then
			local status = "yes"
			RemoteEvent6:FireClient(id_user, status)
		else
			local status = 'no'
			RemoteEvent6:FireClient(id_user, status) -- Unable to cast value to Object
		end
	else
		warn("DATASTORE ERROR: "..failReason)
	end

end)

Yet again, it’s an issue with how you’ve framed your event listener’s parameters. I would highly recommend having a good read through the creator documentation for RemoteEvents and their event listeners to get a good understanding of how events are handled differently on the client and the server.

You have redundancy in your server listener parameters, “player” and “localplayer”. As I previously mentioned, when a client fires an event to the server, the server is already well-aware of exactly where it came from and will automatically and forcefully assign the first parameter of your OnServerEvent() function to the Player instance of the client the remote came from.

For example, if below is my FireServer() call from client to server,
RemoteEvent5:FireServer(“A signal was received”)
Here, I’m sending a string, “A signal was received” as an argument to the server.

In order to correctly listen to this signal on the server, below would be the correct parameters for my event listener for the server:
RemoteEvent5.OnServerEvent:Connect(function(player, message)
Where player indicates the auto-assigned first parameter of the OnServerEvent() function, and message being “A signal was received”.

To fix your issue:
RemoteEvent5.OnServerEvent:Connect(function(player, id_user)

Excuse my messy formatting considering I responded to this on my phone.

Okay but again i get error:

RemoteEvent5.OnServerEvent:Connect(function(player, id_user)
	local data = nil
	local success, failReason = pcall(function()
		data = bank_data:GetAsync("cool_pizza_" ..id_user)
	end)
	if success then
		if data == "yes" then
			local status = "yes"
			RemoteEvent6:FireClient(id_user, status)
		else
			local status = 'no'
			RemoteEvent6:FireClient(id_user, status) -- unable to cast value to object
		end
	else
		warn("DATASTORE ERROR: "..failReason)
	end

end)

After reading some more remote event docs I understand, thanks it works perfectly now.

Final codes:

local function onFocusLost(enterPressed, _inputObject)
	if enterPressed then
		local RemoteEvent4 = game.ReplicatedStorage.RemoteEvent4
		local RemoteEvent5 = game.ReplicatedStorage.RemoteEvent5
		local RemoteEvent6 = game.ReplicatedStorage.RemoteEvent6
		local id_user = Players.LocalPlayer.UserId
		local guess = textBox.Text
		if guess == 'i need cool pizza' then
			textBox.Text = "Wait, checking!"
			RemoteEvent5:FireServer(id_user)
			RemoteEvent6.OnClientEvent:Connect(function(player, status)
				print(status)
				if status == "yes" then
					game.Players.LocalPlayer.PlayerGui.ScreenGui.Redeem_Frame.TextBox.Text = "You already used this code."
					textBox.BackgroundColor3 = colorWrong
				else
					RemoteEvent4:FireServer(player)
					game.Players.LocalPlayer.PlayerGui.ScreenGui.Redeem_Frame.TextBox.Text = "'Cool Pizza' added your bank account. You need rejoin"
					textBox.BackgroundColor3 = colorCorrect
				end
			end)
			RemoteEvent4:FireServer(id_user)
			textBox.BackgroundColor3 = colorCorrect
		else
			textBox.Text = "Invalid Code"
			textBox.BackgroundColor3 = colorWrong
		end
	else
		textBox.Text = ""
		textBox.BackgroundColor3 = colorNormal
	end
end
RemoteEvent5.OnServerEvent:Connect(function(player, id_user)
	print(player)
	local data = nil
	local status
	local success, failReason = pcall(function()
		data = bank_data:GetAsync("cool_pizza_" ..id_user)
	end)
	if success then
		if data == "yes" then
			status = "yes"
		    wait(1)
			RemoteEvent6:FireClient(player, id_user, status)
		else
			status = "no"
			wait(1)
			RemoteEvent6:FireClient(player, id_user, status) 
		end
	else
		warn("DATASTORE ERROR: "..failReason)
	end

end)
1 Like

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