Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Give points on death script is running multiple times?

Ive attempted fixing this but it still doesnt work and now I am stumped.

So I want it to add points when ever a player dies, and it does. But the thing is, if you continue hitting the player after he has died it will continue to add points and I dont know why.

So if you kill the guy and then continue to whack his body [Never thought id say that] it will continue to give you points.

Heres the code.

local Player = script.Parent.Parent.Parent
local EnemyKilled = game.ReplicatedStorage:WaitForChild("EnemyKilled")

script.Parent.BladeHitBox.Touched:connect(function(hit)
	local HitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent or hit.Parent.Parent)
	if script.Parent.CanDamage.Value == true then
		script.Parent.CanDamage.Value = false
		local RandomDamage = math.random(1,3)
		if RandomDamage == 1 then
			if HitPlayer.TeamColor ~= Player.TeamColor then
				hit.Parent.Humanoid:TakeDamage(75)
			end
		elseif RandomDamage == 2 then
			if HitPlayer.TeamColor ~= Player.TeamColor then
				hit.Parent.Humanoid:TakeDamage(80)
			end
		elseif RandomDamage == 3 then
			if HitPlayer.TeamColor ~= Player.TeamColor then
				hit.Parent.Humanoid:TakeDamage(85)
			end
		end
		if hit.Parent.Humanoid.Health < 0 or hit.Parent.Parent.Humanoid.Health < 0 and KillDebounce == false then
			KillDebounce = true
			game.ReplicatedStorage.DataFile[Player.Name].DarthVaderKills.Value = game.ReplicatedStorage.DataFile[Player.Name].DarthVaderKills.Value + 1
			game.ReplicatedStorage.DataFile[Player.Name].TotalKills.Value = game.ReplicatedStorage.DataFile[Player.Name].TotalKills.Value + 1
			game.ReplicatedStorage.DataFile[Player.Name].ExperiencePoints.Value = game.ReplicatedStorage.DataFile[Player.Name].ExperiencePoints.Value + 50
			game.ReplicatedStorage.DataFile[Player.Name].CP.Value = game.ReplicatedStorage.DataFile[Player.Name].CP.Value + 100
			EnemyKilled:FireClient(Player)
			delay(.25, function() 
				KillDebounce = false 
			end)
		end
	end
end)

The stuff to pay attention to is at the bottom, the last if statement. The reason I have to hit.Parents things is because I have parts attached to the player and if they hit a part it still has to register the hit.

Thank you for your time, hopefully we can figure this out :)

1 answer

Theoretically, you're using a inefficient way of adding points every kill. Touched functions are meant to fire every time an object is touched, so in that case every time 'BladeHitBox' is touched, it will give the player points.

A better way to add points is to insert a tag into the player on touch;

local Player = game.Players.LocalPlayer
local Debounce = false

Part.Touched:Connect(function(hit)
	if Debounce == false then Debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(3)
			local tag= Instance.new("StringValue")
			tag.Name = "TagValue"
			tag.Value = Player.Name
			tag.Parent = hit.Parent.Humanoid
		end
	end
end)

After creating the tag that inserts every time you touch the player (note: you must make it go away in a vague amount of time or it would overlap other player's tagvalues!) you need to create a deathfunction for every player that is in the game!


function givePoints(plr)
	if plr then
		plr.Points.Value = plr.Points.Value + 1
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			if humanoid:FindFirstChild("TagValue") then
				local tag = humanoid.TagValue
				givePoints(game.Players[tag.Value])
			end
		end)
	end
end)

That my friend is the correct way of creating a tag, and adding points to a player on death.

2 comments

0

What do you mean make it go away?

0

I have thought of a simpler solution because ive already written like I think 8 scripts with my way of adding points and I dont wanna go back and change them all honestly. What if there was a way you could make body parts go away after the player dies like you know how when a player dies the fall over into a bunch of parts. What if they fell over and instantly got destroyed. Is that possible

This discussion is preserved as it was. New answers, comments, and votes are closed.