I want to get a players name and if they (only they) press T it fires a remote event?
Heres my code:
local OnCooldown = false
local UserInputService = game:GetService(“UserInputService”)
UserInputService.InputBegan:Connect(function(input)
if game.Players.LocalPlayer.Name == “Zvtu” and input.KeyCode == Enum.KeyCode.T then
if OnCooldown == false then
OnCooldown = true
game.ReplicatedStorage.AdminTimeStop:FireServer()
wait(1)
OnCooldown = false
end
end
end)
I am not primarily a scripter so I could use some help.
I’m afraid what you’re trying to do isn’t possible - at least, not possible entirely from the server.
Say you had a local script (in game.ReplicatedFirst or somewhere in game.StarterGUI)
local UserInputService = game:GetService(“UserInputService”)
UserInputService.InputBegan:Connect(function(input, gameEngineProcessedEvent)
if input.KeyCode == Enum.KeyCode.T and not gameEngineProcessedEvent then
if OnCooldown == false then
OnCooldown = true
game.ReplicatedStorage.AdminTimeStop:FireServer()
wait(1)
OnCooldown = false
end
end
end)
And then on your server script:
game.ReplicatedStorage.AdminTimeStop.OnServerEvent:Connect(function(player)
if player.Name == "Zvtu" then
-- Do the thing
end
end)
Would work. For future reference, put 3 ‘Grave’ symbols around code segments (they look like this: `, they’re at the top left of the keyboard)
This script is from a local script from StarterGui.
There are some changes you should make to your code as well as some gotchas to keep in mind:
-
For a maximum degree of effectiveness, you will need to do checking from both the client and the server. The client will vet who is allowed to use actions so that not every client has a stray function connected to InputBegan and the server will use it to check if the player is allowed to act.
-
Use UserIds. Usernames can change and you should be working with UserIds in virtually any case of special user permissions. You can use GetUserIdFromNameAsync under the Players service if you don’t want to find your own UserId but make sure to memoise.
-
The server will automatically receive the player who fired a remote as the first implicit parameter when having a function listen to OnServerEvent. You can find out which player (their instance, not their name) fired off the remote.
-
Try to maintain the habit of code consistency. If you use GetService for one, use GetService for all. Remember that all services are better found by GetService which is also canonical over dot syntax for which not all services may be properly named.
Gotchas aside, your code as it stands is fine enough. You just need to concern yourself with the server side. More information can be found on the Remote Functions and Events article if you’re interested in exploring the networking dynamic and how to use these.
Once again: from the server’s end, you have the player then any passed arguments. All you need to do here is run any conditions you want on the player.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local function onServerEvent(player, ...)
if player.UserId == 152695221 then -- Zvtu
print("Time stop function here")
end
end
ReplicatedStorage.AdminTimeStop.OnServerEvent:Connect(onServerEvent)
Such a script would belong in ServerScriptService.
Ah, my apologies, I posted earlier than I intended to. What I’m trying to say is, have your client fire the event, but then check the client’s name on the server. If you’re trying to reduce network traffic, then yes, by all means check it on the client as well. But the server is the only place that will be able to reliably check if it’s that player, as exploiters can find and fire events without going through local script code.