I need help in highlighting the currentTagged player

  1. Basically i want to make a tag game, and there is currently no way to see who is the tagger, and i want to make a highlight for it, and i have a highlight in the script, but it does not work the way i want it to.

  2. The problem with it currently is that it does not display the tagged player with the highlight when the game starts, and the first tagger needs to tag someone to the highlight to appear in the second tagged player, but when the second tagged player tags someone, the highlight does nothing, the second tagged player just keeps the highlight.

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("DATA STORES3")
local BADGE_ID = 2148716135
local badgeService = game:GetService("BadgeService")

function saveData(plr)

	if not plr:FindFirstChild("FAILED TO LOAD DATA") then
		local cash = plr.leaderstats.Cash.Value

		local success, err = nil, nil
		while not success do
			success, err = pcall(function()
				return ds:SetAsync(plr.UserId, cash)
			end)
			warn(err)
			task.wait(0.1)
		end
	end
end

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		saveData(plr)
	end
end)


game.Players.PlayerAdded:Connect(function(plr)

	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr

	local cashValue = Instance.new("IntValue")
	cashValue.Name = "Cash"
	cashValue.Parent = ls
	
	local success, data = pcall(function()
		return ds:GetAsync(plr.UserId)
	end)

	if success then
		cashValue.Value = data or 0

	else
		local failedValue = Instance.new("StringValue")
		failedValue.Name = "FAILED TO LOAD DATA"
		task.wait(1)
		failedValue.Parent = plr
	end
end)



local rs = game.ReplicatedStorage:WaitForChild("BombTagReplicatedStorage")
local config = require(rs:WaitForChild("CONFIGURATION"))
local maps = rs:WaitForChild("Maps")


local status = Instance.new("StringValue")
status.Name = "STATUS"
status.Parent = rs

local currentTagged = Instance.new("ObjectValue")
currentTagged.Name = "CURRENT TAGGED PLAYER"
currentTagged.Parent = rs

local explodeTimer = Instance.new("IntValue")
explodeTimer.Name = "EXPLODE TIMER"
explodeTimer.Parent = rs



function getPlayers(playerList)

	local playerList = playerList or game.Players:GetPlayers()
	local playersInGame = {}

	for i, plr in pairs(playerList) do
		local char = plr.Parent == game.Players and plr.Character or plr
		if char and char:FindFirstChild("Humanoid") and char.Humanoid.Health > 0 then
			table.insert(playersInGame, char)
		end
	end
	return playersInGame
end


while true do

	local playersInGame = {}

	while #playersInGame < config.MinimumPlayersNeeded do
		playersInGame = getPlayers()

		if #playersInGame < config.MinimumPlayersNeeded then
			local neededPlayers = config.MinimumPlayersNeeded - #playersInGame

			status.Value = "⌛Waiting for " .. neededPlayers .. " more player⌛" .. (neededPlayers ~= 1 and "s" or "")

			task.wait(1)
		end
	end

	for i = config.IntermissionTime, 0, -1 do
		status.Value = "⌛Choosing map in " .. i .. " seconds⌛"
		task.wait(1)
	end

	local chosenMap = maps:GetChildren()[Random.new():NextInteger(1, #maps:GetChildren())]:Clone()
	chosenMap.Parent = workspace

	status.Value = "Now entering " .. chosenMap.Name

	task.wait(config.MapWaitTime)


	playersInGame = getPlayers()

	if #playersInGame < config.MinimumPlayersNeeded then
		break
	else

		local tagDebounce = false
		for i, char in pairs(playersInGame) do

			char.Humanoid.WalkSpeed = config.SurvivorSpeed
			char.HumanoidRootPart.CFrame = chosenMap.SPAWN.CFrame

			char.Humanoid.Touched:Connect(function(hit)
				if not tagDebounce and currentTagged.Value then

					local hitChar = hit.Parent
					if game.Players:GetPlayerFromCharacter(hitChar) then

						if char == currentTagged.Value then
							tagDebounce = true

							char.Humanoid.WalkSpeed = config.SurvivorSpeed
							hitChar.Humanoid.WalkSpeed = config.TaggedSpeed

							currentTagged.Value = hitChar
							if currentTagged.Value then
								local hl = script.Highlight:Clone()
								hl.Parent = hitChar
								else script.Highlight:Destroy()
							end
							

							task.wait(config.TagCooldown)
							tagDebounce = false

						elseif hitChar == currentTagged.Value then
							tagDebounce = true

							hitChar.Humanoid.WalkSpeed = config.SurvivorSpeed
							char.Humanoid.WalkSpeed = config.TaggedSpeed

							currentTagged.Value = char
							if currentTagged.Value then
								local hl = script.Highlight:Clone()
								hl.Parent = char
							end

							task.wait(config.TagCooldown)
							tagDebounce = false
						end
					end
				end
			end)
		end

		local iterations = 0
		while true do
			iterations += 1
			playersInGame = getPlayers(playersInGame)

			if #playersInGame > 1 then
				local randomChar = playersInGame[Random.new():NextInteger(1, #playersInGame)]
				randomChar.Humanoid.WalkSpeed = config.TaggedSpeed

				if iterations == 1 then
					randomChar.HumanoidRootPart.Anchored = true
					

					for i = config.PreparationTime, 0, -1 do
						status.Value = randomChar.Name .. " is tagged💣. You have " .. i .. "s to run."
						task.wait(1)
					end
					currentTagged.Value = randomChar
					status.Value = randomChar.Name .. " is tagged💣"
					if currentTagged.Value then
						local hl = script.Highlight:Clone()
						hl.Parent = currentTagged
					end
					randomChar.HumanoidRootPart.Anchored = false

				else
					currentTagged.Value = randomChar
					status.Value = randomChar.Name .. " got tagged💣"
				end

				local explodesIn = config.BombExplodeTimes[#playersInGame]
				explodeTimer.Value = explodesIn

				local bombTimerStarted = tick()
				while tick() - bombTimerStarted < explodesIn do

					game:GetService("RunService").Heartbeat:Wait()
					explodeTimer.Value = explodesIn - math.round(tick() - bombTimerStarted)

					if not currentTagged.Value or #getPlayers(playersInGame) < 2 then
						break
					end
				end

				if currentTagged.Value and #getPlayers(playersInGame) >= 2 then
					local explosion = Instance.new("Explosion")
					explosion.DestroyJointRadiusPercent = 0
					explosion.Position = currentTagged.Value.HumanoidRootPart.Position
					explosion.Parent = workspace

					currentTagged.Value.Humanoid.Health = 0
					currentTagged.Value = nil
				end
			else
				break
			end
		end

		local winner = game.Players:GetPlayerFromCharacter(playersInGame[1])
		if winner then
			winner.leaderstats.Cash.Value += config.SurviveReward
			status.Value = "👑" .. winner.Name .. " wins!👑"
			badgeService:AwardBadge(winner.UserId, BADGE_ID)
		end

		chosenMap:Destroy()
		for i, plr in pairs(game.Players:GetPlayers()) do
			plr:LoadCharacter()
		end

		task.wait(config.RoundEndTime)
	end
	end

This is the full script

currentTagged.Value = hitChar
if currentTagged.Value then
local hl = script.Highlight:Clone()
hl.Parent = hitChar
else script.Highlight:Destroy()
end

This is the script i use to highlight the player

5 Likes

Instead of cloning a new highlight everytime, why not just use one instance and then parent it to the tagger everytime it changes? Unless the player can die (thus destroying the instance in the process), this should solve the issue.

2 Likes

If there is multiple player the players can die, for example if there is 3 people and someone explodes there will be 2 players left and a random player from that 2 gets the bomb, also how can i use an instance in this type of script? Im not that good at scripting😅

1 Like

Then I recommend to make a function to avoid having to rewrite the same lines of code multiple times.

The way i’d go about this is by looping through every player, check if their character is the tagger and give them or remove from them a highlight instance.

function handleHighlight()
   local players = game.Players:GetPlayers();
   for _, plr in ipairs(players) do
      if plr.Character == nil then continue end;
      if currentTagged.Value == plr.Character then -- the player is the tagger, give the highlight
         local highlight = script.Highlight:Clone();
         highlight.Name = "TaggerHighlight";
         highlight.Parent = plr.Character;
      elseif plr.Character:FindFirstChild("TaggerHighlight") then -- the player is not the tagger, remove the highlight if it exists
         plr.Character.TaggerHighlight:Destroy();
      end;
   end;
end;

Call this everytime a player gets tagged and you should be good. My guess is it should replace this code:

if currentTagged.Value then
   local hl = script.Highlight:Clone()
   hl.Parent = hitChar
else 
   script.Highlight:Destroy()
end
1 Like

It gives an error,

1 Like

Have you made sure you properly set the Adornee property of the highlight to the part you want? Simply setting its parent won’t make it create the highlight over the part.

Refer to the API reference here:

1 Like

Try making it a local function instead of a regular function.

1 Like

The higlight itself works, but when the first tagger tags someone that part works, so the second tagged player gets a red highlight, but when that player tags someone the highlight does not get removed from him and now both players will have the highlight, also when the round starts the first player does not get the highlight

1 Like

Yeah, so in that case just create one highlight and change the Adornee property to whatever you want the highlight to be on (you won’t need to change the parent, just the Adornee).

Doing this is better practice and will ensure only one thing is being highlighted.

1 Like

if currentTagged.Value then
local hl = script.Highlight:Clone()
hl.Adornee = hitChar
end
Like this?

1 Like

Don’t clone the highlight; just use the same highlight.

But yes, that is how you would set the Adornee of the highlight to the part.

Alright, then

if currentTagged.Value then
   local hl = script.Highlight
   hl.Adornee = hitChar
end

So like this?

Yeah, that should work. Try it out and see if it does.

The script itself gives no errors, but in the playtest the highlight does not work

Where is the highlight located? Try monitoring the Adornee value of the highlight and see if it changes.

What do you mean by located? I don’t understand that part

Sorry I mean parented, but from your script, I can see that it is under the script. Is the script in ServerScriptService?

Also try monitoring the Adornee value of the highlight to see if it changes.

Yes the script is in ServerScriptServive, also what do you mean by monitoring?

Test the game and look at the Adornee value of the highlight to see if it changes and what it changes to.

Do i need to give the adornee a parent where it needs to go? Like for an example

if currentTagged.Value then
   local hl = script.Highlight
   hl.Adornee = hitChar.Character
end

edit it didn’t work, and gave me this error