ImageButton accepts input from everyone

So, basically what i want to achieve is that the button can only be pressed by me. If it’s pressed by someone else, do nothing.

This is the button in question:
image

It works as intended, it just accepts input from everyone. What am i doing wrong here?

local imageButton = script.Parent
local Part = script.Parent.Parent.Parent.Parent
local userId = "457870639"

local function playSound()
    local Sound = Instance.new("Sound")
    Sound.SoundId = "rbxassetid://3779053277"
    Sound.Parent = Part
    Sound.RollOffMaxDistance = 200
    Sound.PlayOnRemove = true
    wait()
    Sound:Destroy()
end

imageButton.MouseButton1Click:Connect(function(player)
    local player = game.Players:GetPlayerByUserId(userId)

    if player then
        if script.Parent.Parent.isTrue.Value == false then
            playSound()
            workspace.ReactorStartup.ForceStartupFailure.Value = true
            script.Parent.Parent.isTrue.Value = true
            script.Parent.Parent.TextLabel.Text = "ENABLED"
        else
            playSound()
            workspace.ReactorStartup.ForceStartupFailure.Value = false
            script.Parent.Parent.isTrue.Value = false
            script.Parent.Parent.TextLabel.Text = "DISABLED"
        end
    end
end)
2 Likes

you hardcoded in your UserId
set the userId variable to the players actual UserId (game.Players.LocalPlayer.UserId)

ah ok i just noticed my mistake

2 Likes

You just don’t have a condition for it.

local imageButton = script.Parent
local Part = script.Parent.Parent.Parent.Parent
local userId = "457870639"

local function playSound()
    local Sound = Instance.new("Sound")
    Sound.SoundId = "rbxassetid://3779053277"
    Sound.Parent = Part
    Sound.RollOffMaxDistance = 200
    Sound.PlayOnRemove = true
    wait()
    Sound:Destroy()
end

imageButton.MouseButton1Click:Connect(function(player)

    if player.UserId == userId then
        if script.Parent.Parent.isTrue.Value == false then
            playSound()
            workspace.ReactorStartup.ForceStartupFailure.Value = true
            script.Parent.Parent.isTrue.Value = true
            script.Parent.Parent.TextLabel.Text = "ENABLED"
        else
            playSound()
            workspace.ReactorStartup.ForceStartupFailure.Value = false
            script.Parent.Parent.isTrue.Value = false
            script.Parent.Parent.TextLabel.Text = "DISABLED"
        end
    end
end)

Oh right, i forgot about that, thanks!

1 Like

Well uh, doesn’t work:

I should’ve mentioned this before; script is a ServerScript, not a LocalScript

I’ll take a look at the Developer Documentation to see if i find something.

Pretty sure all you need to debug that is to print(player) before the player.UserId and see if you’re getting the Player reference.
Also why are you handling button clicks on the server? There is absolutely no need to do so, make a local script and fire an event when needed to the server to do anything you need on the server

ImageButton is in a SurfaceGui part, not an ScreenGui, so how can i do that?

For one, i certainly know that it’s just not :FireServer(), it fires to the clientside and yeah doesn’t return anything- oh wait

yep, i can just use a return(). i forgot about those.

1 Like

Either Way, handle it on the Client by Referencing it on the local script

local Part = workspace:WaitForChild("Part")
local SurfaceGui = Part:WaitForChild("SurfaceGui")
local StartupFailure = SurfaceGui:WaitForChild("StartupFailure")
local Button = StartupFailure:WaitForChild("Button")

Button.Activated:Connect(function()
     --> Do Something
end)

I’ll be honest, i didn’t know that i could read events from replicatedstorage while being in the same directory of the firing script. This is good to know, thanks!

The code that I provided above pretty much has nothing to do with ReplicatedStorage, let me know what you’ve understood so I can help clear some concepts for you

So i place the event on the same parent of both scripts? Or what do i do exactly? This is confusing.

What do you mean by Event here?

1 Like

RemoteEvent.
Basically. from what i understood, i have to do this:

  • get userID via localscript
  • return it to serverscript
  • execute actions accordingly

Are events even necessary here or am i being stupid?

Alright so to make Events simpler to understand, you do the Following:

  • You Make a RemoteEvent and store it in ReplicatedStorage.
  • Assuming You want to send the Event from the Client to the server, You would do the following on the client
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent:FireServer("Payload")
  • Reference the Same RemoteEvent on the Server and Attach a callback to it
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(player, payload)
	print(payload)
end)

Are events even necessary here or am i being stupid?
Yes. Events Are Absolutely Necessary because You don’t want everything available on the client as exploiters can see this data. Any time you need to either make the server do some work for you (RemoteEvents) or ask the server some question that you don’t want the answer to stored on the client (RemoteFunction) you use Remotes (Whichever suits your use-case)

Well, i did know how events worked; no offense… but i just wanted to know what i had to do there.

I suppose i can place the localscript on ScreenGui as apparently LocalScripts don’t work in Workspace (and why would i place it there) and just get the playerID of the player that clicked the button and bla bla bla… you get the idea

I would suggest putting the Localscript in StarterPlayerScripts and referencing the Gui from there as I suggested in my previous reply

Okay, did that.

local players = game:GetService("Players")

local Part = workspace:WaitForChild("ServerSettingsScreen")
local SurfaceGui = Part:WaitForChild("SurfaceGui")
local StartupFailure = SurfaceGui:WaitForChild("StartupFailure")
local Button = StartupFailure:WaitForChild("Button")

local store = game:GetService("ReplicatedStorage")
local event = store:WaitForChild("CheckUserID")

Button.Activated:Connect(function()
	local player = players.LocalPlayer
	if player then
		local userId = player.UserId
		event:FireServer(userId)
	end
end)

I think this is how to do it?

You don’t really need a RemoteEvent here, You can get the players UserId on the client

local players = game:GetService("Players")
local player = players.LocalPlayer
local userId = 457870639
local Part = workspace:WaitForChild("ServerSettingsScreen")
local SurfaceGui = Part:WaitForChild("SurfaceGui")
local StartupFailure = SurfaceGui:WaitForChild("StartupFailure")
local Button = StartupFailure:WaitForChild("Button")

local store = game:GetService("ReplicatedStorage")
local event = store:WaitForChild("CheckUserID")

Button.Activated:Connect(function()
	if player.UserId == userId then
		--> Do the Other Stuff here
	end
end)

Thing is, the actions have to be performed on the Server, not locally, since i want everyone to experience the change.

local players = game:GetService("Players")
local player = players.LocalPlayer
local userId = 457870639
local Part = workspace:WaitForChild("ServerSettingsScreen")
local SurfaceGui = Part:WaitForChild("SurfaceGui")
local StartupFailure = SurfaceGui:WaitForChild("StartupFailure")
local Button = StartupFailure:WaitForChild("Button")

local store = game:GetService("ReplicatedStorage")
local event = store:WaitForChild("CheckUserID")

Button.Activated:Connect(function()
	if player.UserId == userId then
		--> Do the Other Stuff here
                event:FireServer() --> Include anything you want to send to the server between these brackets
	end
end)

Honestly, I would suggest you learn RemoteEvents before diving into this, Refer to the Message I sent above, there’s working code for RemoteEvents that you can use to get familiar with it before you try doing what you actually want to do