Im trying to make a dragon game and i currently need help with a ImageButton. Upon joining, the player obtains a firedragon egg which is basically created via
Instance.new("ImageButton")
to the player's inventory GUI when pressing the "Claim!" Button.
Now the player i supposed to go to the hatching machine where he has to press on the ImageButton(The firedragon egg) and the dragon will hatch. However, the whole Joining-present event is inside a localscript, while the hatching event is inside a serverscript inside of the machine (inside a clickdetector). I know 2 solutions to this: 1. Create a RemoteEvent(FireServer). But i do not understand that whole thing at all.
function parentPressed() script.Parent.click.Value = true local plr = game.Players.LocalPlayer if plr.leaderstats.Food.Value == 0 then plr.leaderstats.Food.Value = (plr.leaderstats.Food.Value + 100) if plr.leaderstats.Gold.Value == 0 then plr.leaderstats.Gold.Value = (plr.leaderstats.Gold.Value + 500) --Give Dragon Egg local fireegg = Instance.new("ImageButton") fireegg.Parent = game.Players.LocalPlayer.PlayerGui.Inventory.Frame fireegg.Name = "FireDragon" fireegg.Size = UDim2.new(0,100 , 0,100) fireegg.Position = UDim2.new(0.031,0 , 0.044,0) fireegg.Image = "rbxgameasset://Images/dragonfireegg" fireegg.BackgroundTransparency = 1 fireegg.ZIndex = 3 fireegg.Visible = true local hatchwho = game.ReplicatedStorage.HatchWho:Clone() hatchwho.Parent = fireegg --Dragon Part End script.Parent.Frame1.Position = script.Parent.Frame2.Position script.Parent.Frame2.Visible = false wait(0.2) script.Parent.Frame1.Position = script.Parent.Frame3.Position script.Parent.Frame2.Visible = true wait(0.2) script.Parent.Parent.Visible = false wait(5) script.Parent.click.Value = false end Rem:FireServer(parentPressed) end end end script.Parent.MouseButton1Down:connect(parentPressed)
I gotchu man. So replicated storage and remote events are a way for local scripts and normal scripts to talk to each other. Normally they can’t communicate and share data but with remote events they can.
Replicated storage is the place that remote events reside. You create a remote event inside replicated storage and name it whatever you want, usually something to do with what data you are sending.
So how do you use remote events? First you Should make a variable that is set to the remote event you created. So
Local Event = game.ReplicatedStorage.WhateverTheNameIs
It’s important to note you can also create remote events in the script without placing it in replicated storage. But I’ll leave that for you to look up.
Now that that is done, there are two ways you can send data, to the server or to the client.
To the client means you are sending data from a normal script to a local script. To the server is the opposite.
So in your case what you would want to do is have a connect function that runs when the image button is clicked on a local script you want to fire to the server. I don’t know the exact circumstances so I’ll give you an example of firing to the server instead.
Let’s say I want something to happen in the game when I click a GUI button. So naturally in the local script in the GUI I would listen for a player to click the button, and then when that happens I would run whatever I wanted to happen. So it would look something like this:
player.PlayerGui.TextButton.MouseButton1Click:Connect(function() end)
So let’s say I want something to happen in the game or I want to send data to the server to save. You would just fire an event inside that function. Like so
player.PlayerGui.TextButton.MouseButton1Click:Connect(function() Event:FireToServer(Data) end)
I’d have to look but I forget if it’s FireToServer. Event is the name of the event you made up at the top where I said local Event = blah
It’s important to note that when you are firing from the client to the server, the first parameter is always player. So it’s much like calling a function.
Let’s say you have a function named local function BLAH(Data1,Data2).
And you call it by saying BLAH(player, Data).
That’s exactly what You are doing when you fire to the server. Except the parameter player is being passed automatically.
So now that the client side is done let’s hop over to the Server script.
The server script must make an event to store the remote event you made in Replicated Storage. So exactly like we did in the local script
local Event = game.ReplicatedStorage.WhateverTheNameIs
Now to receive that data you need to have a Connect
Event:OnServerEvent:Connect(function(player, Data) end)
So since the client sends player as a parameter automatically it’s important to have it in the functions parameters like above. I have two variables in the parenthesis instead of one like in the local script.
It’s important to note: you can name those values anything you want, all they do is receive what you sent. So instead of player it can be plr or instead of Data it can be YEEEET.
Then all you have to do is use the data you sent . Or type the code to hatch the egg in the function and don’t send any data other than player.