How to run a function/script on a specific player?

Hi, I am making this survival game, where a random player gets picked to become the murderer to kill survivors, in order to do this, I have to give certain scripts and functions to the player but I do not really know how.

Sometimes when I code in Roblox Lua, I have no idea what I am doing.
Here’s what I have so far:

local players = game:GetService("Players"):GetPlayers()
local picked = players[math.random(1, #players)]

There seems to be more details left out. Do you have any scripts that discerns and separate between what makes a player the murderer? What do you expect to happen?

When the player becomes the murderer, special keybinds are given to them used to eliminate survivors.

1 Like

That sounds like something called colloquially as ‘abilities’. Maybe you’re looking for a few components here:

  1. The component for clients to respond to the keybinds, firing RemoteEvents associated with them
  2. Components that listen and executes the said abilities
  3. Visual components as an option on client

Unfortunately, I do not know exactly how you want it. You choose the design.

1 Like

Those options are close to my goal, yes the abilities are only on the selected player’s client.

for instance: if the player presses “F”, An attack animation plays with it’s hitbox like:

local players = game:GetService("Players"):GetPlayers()
local picked = players[math.random(1, #players)]
local pickedchar = picked.Character or picked.CharacterAdded:Wait()
local Humanoid = pickedchar.Parent:FindFirstChild("Humanoid")


local key = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10544400098"

key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		local animationTrack = Humanoid:LoadAnimation(animation)
		animationTrack:Play()
		-- attack code
	end
end)

Of course, I want to do this only on the selected player.

1 Like

The client can detect a change theirself and run a function, or the server can specifically tell the client to run this function. So the remote event would tell every player what their role is, and the client would tell the player.

local Frame = script.Parent.OxFrame
local Bar = Frame.OxBar
local Lable = Frame.OxLabel
local Player 
local Stoping = false

game.Workspace.O2Lose.Touched:Connect(function(hit)
Player = game.Players.LocalPlayer
if hit and hit.Parent:FindFirstChild("Humanoid") then
   if Player.Name == hit.Parent.Name then


	if Stoping then
		return
	end
	Stoping = true
	for i = 442 , -55  , -5 do
		Bar.Size = UDim2.new(0, i,0, 35)
		
		Lable.Text -= 1
		
		wait(0.01)
	end
	if Lable.Text == "0" then
		while wait(0.5) do
			Player.Character.Humanoid:TakeDamage(10)
		end
	end
    end
end
end)

You really only need to check the player name with whoever touched the part.

I am sorry but this is not even close to what I want

2 Likes

Since the abilities are given by connecting the ability function to the user input, you can get the server to fire a RemoteEvent to the chosen player, and in a local script you can code it so that, if that RemoteEvent is fired for this player, bind all the special abilities

example code, in a local script (given to every player)

local remote = game.ReplicatedStorage:WaitForChild('RemoteEvent') -- name the remote whatever

remote.OnClientEvent:Connect(function()
    key.InputBegan:Connect(function(input)
	    if input.KeyCode == Enum.KeyCode.F then
	    	local animationTrack = Humanoid:LoadAnimation(animation)
    		animationTrack:Play()
    		-- attack code
    	end
    end)
end)
3 Likes

thank you, now I finally understand

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