Make this randomly selected player take damage

This will be an easy task that someone else should know how to help me with. I’m making a story game and it’s gonna have one of those generic poisoned food gimmicks that get overused. I’m not gonna get fancy and give the players a tool to use, instead I’ll just make the food disappear and assume that every player has eaten it. Then the next player that gets randomly selected to speak will be the one to take damage. I just need that randomly selected player to take damage.

Here is the uncopylocked place if you think you got what it takes to fix it for me:
https://www.roblox.com/games/7548342515/Make-the-randomly-selected-player-take-damage

Just send me back a copy of the place after you got it working the way it should. Also you are allowed to freely keep and use the assets in the uncopylocked place as long as you can help me out. Thanks!

Is this what you want to do?

--READ THIS FIRST

--Make it so that the random player that speaks takes damage also. The random player is at line 80.

--By the way you are free to use and copy everything in here, I won't mind as long as you can help me out.






local Players = game:GetService("Players")

local CreateDialogueEvent = game.ReplicatedStorage.CreateDialogueEvent
local TimerEvent = game.ReplicatedStorage.TimerEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local EndFadeEvent = game.ReplicatedStorage.EndFadeEvent
local EndEvent = game.ReplicatedStorage.EndEvent
local playSoundEvent = game.ReplicatedStorage.playSoundEvent
local stopSoundsEvent = game.ReplicatedStorage.stopSoundsEvent


local randomPlayerName
local randomPlayerId



local function getPlayerImage(player_id)
	local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(player_id, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	return content
end

local function getRandomPlayer()
	local players = game.Players:getPlayers()
	local howMany = #players
	if howMany == 0 then
		return
	end
	local number = math.random(1,#players)
	local randomPlayer = players[number]
	randomPlayerName = randomPlayer.Name
	randomPlayerId = randomPlayer.UserId
end

function teleportPlayers(partCFrame)
	local players = game.Players:getPlayers()
	for _,player in pairs(players) do
		if player.Character then
			if player.Character.HumanoidRootPart.Anchored == false then
				player.Character.Humanoid.Jump = true
			end
		end
	end
	wait(.5)
	for _,player in pairs(players) do
		if player.Character then
			if player.Character.HumanoidRootPart.Anchored == false then
				player.Character:SetPrimaryPartCFrame(partCFrame)
			end
		end
	end
end



----------------------------------------


local function challenge_1()

	getRandomPlayer()
	CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"Let's eat these snadwhiches that are totally not poisoned!")
	wait(8)

	game.Workspace.Sandwhiches:Destroy()
	wait(4)

	getRandomPlayer() --This is what randomly selects a new player, now how do I make this randomly selected player take damage?
	local player = Players:GetPlayerByUserId(randomPlayerId)
	if player then
		local Humanoid = player.Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(50)
		end
		CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"I took 50 damage, that's what I would say if I actually took damage...")
		wait(20)
	end

end

--------------------------------------------------

local function startGame() 
	challenge_1()
end

wait(6)

while #Players:GetPlayers() < 1 do
	Players.PlayerAdded:Wait()
end
print("Ready to start game")

startGame()
1 Like

Let me test it out to make sure it works correctly.