How to make script only work for chosen player?

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)

In the server script:

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.

Is this placed before userInputService is stated? or before keyCode?

Ah, what I posted was the LocalScripts (client)

This is the ServerScript that connects to it:

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)

Which player do you want to modify the light?

I want only myself to be able to use it.
Should I use either waiting for it to detect me via WaitForChild, or by userId?

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)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.