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

How to make a working gui button using onclicked()?

Asked by 5 years ago

Just start i'm really new with scripting so sorry if some of my questions are really easy.

I've been trying to make a rocket ship and in order to launch it I want to use an on screen button that is clicked to start the launching process.

Located in Startgui I have ScreenGui > TestButton > Script

The script contains

function onClicked() print("Orefef") end script.Parent.MouseButton1Click:connect(onClicked)

Whenever I click on the button, "Orefef" does not print so obviously the button does not work. Probably a really dumb mistake but any help would be great.

0
LocalScript Ziffixture 6913 — 5y

2 answers

Log in to vote
-1
Answered by 5 years ago

Make sure to put the script in a local script, since this is client side and it should work!

Ad
Log in to vote
2
Answered by 5 years ago

Like Feahren said in the comments, you need to use a LocalScript for the button to work from a Gui (these are client specific)

Local Script

script.Parent.Activated:Connect(function()
    print("Orefef")
end)

Activated is preferred since it works on all devices, not just computers.

This script is fine if you want only the player that clicked the button to see the launch, otherwise, if all players will see it, you need to use Remote Functions & Events as outlined here: https://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events

Example of Remote Event

Local Script

local remote = game.ReplicatedStorage.RemoteEvent

script.Parent.Activated:Connect(function()
    print("Orefef")
    remote:FireServer()
end)

Server Script

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(player)
    -- Causes Rocket to Launch
end)

For the above example, I have a Remote Event in Replicated Storage named "RemoteEvent"

0
Thanks Alexanders1123 16 — 5y
0
Okay so i'm testing the scripts that you guys gave me right now, and the "print" works however my rocket command wont. I'm using game.Workspace.Rocket.Boosters1.Velocity=game.Workspace.Rocket.Boosters1.CFrame.lookVector * 700 to launch it, so perhaps there's something wrong with that? Alexanders1123 16 — 5y
0
Sure, firstly, are you using a Remote Event or are you using only a Local Script? Secondly, the rocket is a model and boosters1 is a part in the model? If it is a model, do you have a Primary Part set? SerpentineKing 3885 — 5y

Answer this question