I have looked on the Developer Hub, but I couldn’t find my answer.
local Buy = game.StarterGui.Shop.Background.StarShop.SkipStage.Buy
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Function
Buy.MouseButton1Click:Connect(function(player)
ReplicatedStorage.Shop.StarShop.SkipStage:FireClient(player)
print("Eventfired")
end)
The script above should fire a RemoteEvent named ‘‘SkipStage’’, when the person clicked on the ‘‘Buy’’ button from the shop, it should fire the event.
I’m just a beginner and I would like help. It is very much appreciated.
Edit: if you need more information, feel free to ask.
local Buy = script.Parent
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("SkipStage")
Buy.MouseButton1Click:Connect(function()
event:FireServer()
print("Server event fired")
end)
You cannot detect mouse-clicks from a server-side script in a GUI.
Make the above script a LocalScript and place it as a child of your Buy button, then it will fire the server.
I’m also assuming SkipStage is a RemoteEvent in ReplicatedStorage, so now you just need to make a server-script to finalise the purchase when the RemoteEvent is received.
First, create a localscript inside of the Buy button and put the code like this:
local buyBTN = script.Parent -- Instance to the Buy button
local Remote = -- Instance to the Remote
buyBTN.MouseButton1Click:Connect(function() --This doesn't have a parameter
Remote:FireServer() -- This contains an automatic parameter
end)
Next, on the normal Script should contain:
local Remote = -- Instance to the Remote
Remote.OnServerEvent:Connect(function(playerSender)
print(playerSender.Name, "called the Remote!") -- Sorbious called the Remote!
end)
The playerSender is a default parameter once the Remote has been called.
Now you need to include the player variable inside the parentheses to ensure it only completes the action for the player who fired the RemoteEvent (you only need to include this in the server-script).
I’d also recommend you changing the Instance in the LocalScript I provided to where your RemoteEvent actually is in ReplicatedStorage as I didn’t realise it was in a different directory.
Yes, I’d recommend predefining the variables in-case the script loads in before the Instances, however.
local RS = game:GetService("ReplicatedStorage")
local Shop = RS:WaitForChild("Shop")
local StarShop = Shop:WaitForChild("StarShop")
local event = StarShop:WaitForChild("SkipStage")
event:OnServerEvent:Connect(function(player)
print("Server fired by".. player.Name)
-- code here
end)
Sorbious and waIshulk:
Both of your answers worked and helped me a lot: thank you for your time, and sharing your knowledge with other people (me in this case).
I can’t mark both as solution (even though they are both solution-worthy), so I will mark the first reply as solution (post by waIshuk). Sorry for that.
But thanks to both of you this script works now. Thank you so much!