Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

A remote event is being fired, but it's not being received. Help?

I recently asked a question asking how to have a script fire the remote event, that is working, but now I need help with the receiving script. This is the working script:

local SandSell = workspace.Events.SandSell
local function Touched(Part)
local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
if Player then
SandSell:FireClient(Player)
print("Sell function has been sent")
end
end
script.Parent.Touched:Connect(Touched)

This is the non-working script:

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local NonShow = game.Players.LocalPlayer:WaitForChild("NonShowing")
local SandSell = workspace.Events.SandSell
SandSell.OnClientEvent:Connect(function(Player)
print("Event Was Received")
if NonShow.BPS.Value > 0 then
print("Backpack Value is Greater than 0")
leaderstats.Cash.Value = leaderstats.Cash.Value + NonShow.BPS.Value
print("Sold")
NonShow.BPS.Value = 0
print("Reset BP Value")
else
end
end)

Can I get any help? I don't understand what I'm doing wrong.

4 comments

0

Formatting got all ripped up in the code blocks, It's actually make correctly in Studio

0

Could you be more specific? Which parts exactly are not working? Is it printing things outside of the OnClientEvent? Is it not printing things inside of the event?

0

Nothing is being printed from the second script, I'm unsure which part isn't correct.

0

I'll accept an answer when it works for me.

2 answers

✓ Accepted answer

It is because you're listening to the wrong event on the server, and firing the wrong thing on the client. As client you send event invokes with FireServer. You don't need to give the player with it, the game does that automatically. Assuming you're working in a LocalScript, that would explain why FireClient did nothing.

local SandSell = workspace.Events.SandSell
local function Touched(Part)
local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
if Player then
SandSell:FireServer()
print("Sell function has been sent")
end
end
script.Parent.Touched:Connect(Touched)

As server you listen to OnServerEvent, which comes with the Player who called it. Whereas OnClientEvent is what clients listen to, this does not come with a Player in the arguments.

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local NonShow = game.Players.LocalPlayer:WaitForChild("NonShowing")
local SandSell = workspace.Events.SandSell
-- Below is the line I've changed
-- from OnClientEvent to OnServerEvent
SandSell.OnServerEvent:Connect(function(Player)
print("Event Was Received")
if NonShow.BPS.Value > 0 then
print("Backpack Value is Greater than 0")
leaderstats.Cash.Value = leaderstats.Cash.Value + NonShow.BPS.Value
print("Sold")
NonShow.BPS.Value = 0
print("Reset BP Value")
else
end
end)

The naming for calling and receiving may seem confusing but remember it like this: What is in the name is what you are calling to, not from.

5 comments

0

I tried this script and it didn't work either, any clues?

1

My bad, I missed the error in your first script. You've switched the fire and listeners around. I've updated my post to reflect that.

0

I just tried this as I was clearing out some of my older things, didn't notice the edit. It didn't work. I got the error:

0

18:18:47.984 - FireServer can only be called from the client

0

None of these worked for me. I don't know if there is a setting that I need to change for it to work.

In all honesty I would say to use ReplicatedStorage instead of Workspace for holding it.

-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SandSell = ReplicatedStorage:WaitForChild("SandSell")

local function Touched(Part)
	local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
	if Player then
		SandSell:FireClient(Player)
		print("Sell function has been sent")
	end
end

script.Parent.Touched:Connect(Touched)

Then for the ServerScript (In ServerScriptService)

-- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SandSell = Instance.new("RemoteEvent", ReplicatedStorage)
SandSell.Name = "SandSell"

local function functionName(Player)
	local leaderstats = Player:WaitForChild("leaderstats")
	local NonShow = Player:WaitForChild("NonShowing")
	print("Event was received")
	if NonShow.BPS.Value > 0 then
		print("Backpack Value is Greater than 0")
		leaderstats.Cash.Value = leaderstats.Cash.Value + NonShow.BPS.Value
		print("Sold")
		NonShow.BPS.Value = 0
		print("Reset BP Value")
	end
end

SandSell.OnServerEvent:Connect(functionName)

3 comments

0

This didn't work for me, It prints the Sell function has been sent part, but idk if it got received. I'll look into it a bit more.

0

I noticed that there is no checking if the event was fired in the second script, I don't know how to fix it.

1

Sorry! I forgot about that part. I will fix it right now.

This discussion is preserved as it was. New answers, comments, and votes are closed.