I have a script that enables a light source via key binds, but people that can press that assigned key can also activate it, not only me.
Is there a way I can make it so that the script chooses a player, and only allows that player to run it?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LightStart = ReplicatedStorage:WaitForChild("LightStart")
local LightStop = ReplicatedStorage:WaitForChild("LightStop")
local userInputService = game:GetService("UserInputService")
local light = game.Workspace.RideVehicle.headlight1
userInputService.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.KeypadMinus then
game.ReplicatedStorage.LightStart:FireServer(light)
end
end)
userInputService.InputEnded:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.KeypadMinus then
game.ReplicatedStorage.LightStop:FireServer(light)
end
end)
local chosenPlayer = ...
game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player, light)
if player ~= chosenPlayer then return end
--Code
end
FYI, you can do it on the client but it would be exploitable.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local lights = {
workspace.RideVehicle.headlight1,
workspace.RideVehicle.headlight2,
workspace.RideVehicle.headlight1.att.light,
workspace.RideVehicle.headlight2.att.light
}
local startColor = Color3.new(1, 1, 1)
local endColor = Color3.new(0, 0, 0)
local fadeTime = 1
local lightAnimations = {}
for _, light in lights do
lightAnimations[light] = TweenService:Create(light, TweenInfo.new(fadeTime),{Color = endColor})
end
function fade(light)
local tween = lightAnimations[light]
if lightAnimations[light] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
tween:Play()
end
end
game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player, light)
local tween = lightAnimations[light]
if lightAnimations[light] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end
light.Color = startColor
end)
game.ReplicatedStorage.LightStop.OnServerEvent:Connect(function(player, light)
fade(light)
end)
game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player, light)
if player.UserId ~= YOUR_USER_ID_HERE then return end
local tween = lightAnimations[light]
if lightAnimations[light] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end
light.Color = startColor
end)