Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

0
Formatting got all ripped up in the code blocks, It's actually make correctly in Studio superawesome113 112 — 5y
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? chomboghai 2044 — 5y
0
Nothing is being printed from the second script, I'm unsure which part isn't correct. superawesome113 112 — 5y
0
I'll accept an answer when it works for me. superawesome113 112 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
I tried this script and it didn't work either, any clues? superawesome113 112 — 5y
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. NLferdiNL 21 — 5y
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: superawesome113 112 — 5y
0
18:18:47.984 - FireServer can only be called from the client superawesome113 112 — 5y
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. superawesome113 112 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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

```lua -- 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)

```lua -- 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)

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. superawesome113 112 — 5y
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. superawesome113 112 — 5y
1
Sorry! I forgot about that part. I will fix it right now. Warriorfoox 25 — 5y

Answer this question