I want to fire a remote event that opens a GUI but it seems that the remote event is either not firing or the client is not receiving it correctly, I tried with a bindeable event and it works, it seems like the problem is with the communication with the server to the client, but I can’t figure out what is it.
Here’s my script
--Service
local CollectionSevice = game:GetService("CollectionService")
--Event
local openShopEvent = game.ReplicatedStorage.Shop.OpenShop
local Shop = {}
Shop.__index = Shop
Shop.TAG = "Shop"
function Shop.new(shop)
local self = {}
setmetatable(self,Shop)
self.shop = shop
self.shopTier = shop.Name
self.debounce = false
self.touchConnection = shop.Touched:Connect(function(...)
self:OnTouch(...)
end)
return self
end
function Shop:OnTouch (part)
if self.debounce then return end
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
Shop:OpenShop(player,self.shopTier)
self.debounce = true
end
end
function Shop:OpenShop(player,shopTier)
if self.debounce then return end
print("Opening shop tier: " .. shopTier .. " for player: " .. player.Name)
openShopEvent:FireClient(player, shopTier)
end
function Shop:CleanUp()
self.touchConnection:Disconnect()
self.touchConnection = nil
end
local shops = {}
local shopAddedSignal = CollectionSevice:GetInstanceAddedSignal(Shop.TAG)
local shopRemovedSignal = CollectionSevice:GetInstanceRemovedSignal(Shop.TAG)
local function onShopAdded(shop)
shops[shop] = Shop.new(shop)
end
local function onShopRemoved (shop)
if shops[shop] then
shops[shop]:CleanUp()
shops[shop] = nil
end
end
for _, inst in pairs(CollectionSevice:GetTagged(Shop.TAG)) do
onShopAdded(inst)
end
shopAddedSignal:Connect(onShopAdded)
shopRemovedSignal:Connect(onShopRemoved)
and here’s the local script which receives the event
local openShopEvent = game.ReplicatedStorage.Shop.OpenShop
local function openShop (player, shopTier)
print("Opening shop")
player.PlayerGui.MainGui.Shop.Visible = true
end
openShopEvent.OnClientEvent:Connect(openShop)
It looks correct to me. What are you seeing in the output? Do you see the server getting to the “Opening shop tier: …” part?
Also–and this doesn’t affect whether it works or not–on the client side of things, the first argument will not be the player, but should be the shopTier. The player argument isn’t passed along to the client with RemoteEvents.
@sleitnick Is likely right about player causing your localscript to not work. You don’t need player at all in the function, as .OnClientEvent does not pass the player value, it’s only for the Server-Side to know which client to fire the event to.
Instead, try this:
local openShopEvent = game.ReplicatedStorage.Shop.OpenShop
local player = game.Players.LocalPlayer
local function openShop(shopTier)
print("Opening shop")
player.PlayerGui.MainGui.Shop.Visible = true
end
openShopEvent.OnClientEvent:Connect(openShop)
So, in case you still want to know what exactly would happen in the first example, then here should be the explanation.
The openShop function is confusing player with shopTier which you’re passing from the server script. Thus, it’s attempting to do shopTier.Player.MainGui.Shop.Visible = true which would not work.
And, if this doesn’t fix it, let us know - everything else seems fine to me.
Made the changes but still nothing, this has never happened to me before, the print isn’t even showing in the console (local print)
local openShopEvent = game.ReplicatedStorage.Shop.OpenShop
local LocalPlayer = game.Players.LocalPlayer
local function openShop (shopTier)
print("Opening shop")
LocalPlayer.PlayerGui.MainGui.Shop.Visible = true
end
openShopEvent.OnClientEvent:Connect(openShop)
Hmmm, is your script/localscript disabled? Try doing a very simple RemoteEvent fire and receive from the same script and localscript, but with no parameters and outside of any other code. Simply fire to the client, and then print something. But first, try this instead in the localscript, just as a test:
Just made a simple script firing it when touching a part and it didn’t fire to client, there’s definitely something happening in the local script, I tried with the anonymous function but still it didn’t fire.
local popUI = game.ReplicatedStorage.GrappleSystem.PopUI
local openShopEvent = game.ReplicatedStorage.Shop.OpenShop
--Services
local TweenService = game:GetService("TweenService")
local kineticEnergyText = script.Parent.Main.KineticEnergy.KineticEnergy
local EnergyGained = script.Parent.Energy
local localPlayer = game.Players.LocalPlayer
local FetchDataFunction = game.ReplicatedStorage.ProfileService.FetchData
local scoreDisplayModule = require(game.ReplicatedStorage.ScoreDisplay)
-----------------------------------------Update U.I Values----------------------------------
kineticEnergyText.Text = scoreDisplayModule.DealWithPoints(localPlayer:WaitForChild("leaderstats"):WaitForChild("K. Energy").Value)
localPlayer.leaderstats:WaitForChild("K. Energy").Changed:Connect(function()
kineticEnergyText.Text = scoreDisplayModule.DealWithPoints(localPlayer:WaitForChild("leaderstats"):WaitForChild("K. Energy").Value)
end)
-----------------------------------------Functions-----------------------------------------------
local function popValueGained (valueGained)
local EnergyGainedGUI = EnergyGained:Clone()
EnergyGainedGUI.Parent = script.Parent
EnergyGainedGUI.Visible = true
local xPos = math.random(0,1)
local yPos = math.random(0,1)
EnergyGainedGUI:TweenPosition(script.Parent.Main:WaitForChild("KineticEnergy").Position, Enum.EasingDirection.In, Enum.EasingStyle.Bounce,2,true)
EnergyGainedGUI.Text = "+".. scoreDisplayModule.DealWithPoints(valueGained).. " Energy"
wait(2)
EnergyGainedGUI.Visible = false
EnergyGainedGUI.Position = UDim2.new(xPos, 0,yPos, 0)
end
local function openShop (shopTier)
print("Opening shop")
localPlayer.PlayerGui.MainGui.Shop.Visible = true
end
--Connect events
popUI.OnClientEvent:Connect(popValueGained)
openShopEvent.OnClientEvent:Connect(openShop)
Here’s the whole local script which receives the event, I can’t figure out what’s wrong.
It is working, it assigns the data values for the UI, it is at StarterGui. It just doesn’t receive that event I’ll try to put the event in another local script. [EDIT] I created a new local script and just put the code for the event on it and it worked, strange, I don’t know why but it worked