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:
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)
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)
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
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
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
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)
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)
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