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.
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.
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)