Setting gui text with concatenation only works once

I want my text label to increase the kill value text by one every time a kill is gained in the leaderstats

The issue is my script only increases the kill value once and doesn’t seem to work after that (it works if the script resets to 1 kill, then after reaching 2 kills is the part where the script breaks)

I have looked in the devforum but it seems like once again, i’m the only one with this problem

The code i made:

Kills.Changed:Connect(function(newKill)
	killGui.KillText.Visible = true
	if newKill > oldKill and newKill - oldKill == 1 and not Running then
		killGui.KillText.Text = "+"..newKill - oldKill.." kill!"
	end
	if newKill > oldKill and newKill - oldKill == 1 and Running then
		newKill += 1
		killGui.KillText.Text = "+"..newKill - oldKill.." kills!"
		newKill -= 1
	end
end)

(this is a shortened version of my script, the “Running” variable just acts as a breaker so the if statements work as they should, i will post the extra code if needed)

Hmmm i can’t see you changing old kill somewhere in code. So i think it happens that
newkill is 2 and oldkill is 0, so 2-0 = 2.

mb forgot about that (old kill default is 0)

Kills.Changed:Connect(function(newKill)
	killGui.KillText.Visible = true
	if newKill > oldKill and newKill - oldKill == 1 and not Running then
		killGui.KillText.Text = "+"..newKill - oldKill.." kill!"
	end
	if newKill > oldKill and newKill - oldKill == 1 and Running then
		newKill += 1
		killGui.KillText.Text = "+"..newKill - oldKill.." kills!"
		newKill -= 1
	end
    oldKill = newKill
end)

New reply because your reply was edited, the second if statement does not run unless the first if statement runs first, there is a tick() underneath the if statements and while it is active it makes the running variable true, therefore the second if statement cannot be ran at the first kill update

Please correct me if I’m wrong, but I’m pretty sure the argument for new value for Changed and :GetPropertyChangedSignal() are broken and don’t seem to work. So, you have to define the new value yourself in the function.

what? when were they ever broken?

By broken, I mean the event works good. But, the issue is that the argument sometimes work and sometimes doesn’t. That’s why, you have to use:

Instance.Changed:Connect(function(newValue --[[Doesn't always provide the new value and sometimes return nil]])
    local newVal = Instance.Value -- This returns the new value always.
end)

yeah i get what you mean but thats not my problem and i never had that problem lol

By the way, I might be wrong but should it be newKill - oldKill > 1?

Epic change of topic : )

no because for example if the player got 8 kills and the old kill value was 7 it would remain with 1 because we are subtracting the newKill with the oldKill, i would have to use > if there was explosives or aoe in my game

Did you try debugging in the event to check if it still works the next time?

yes, i placed a print() under the second if statement to make sure its not on my end and it still prints, meaning the conditions were met

I can’t understand why it’s not working because according to me, the code seems fine.

ok so i smelled some gas and i found out the fix:

local numberIncrease = 0
Kills.Changed:Connect(function(newKill)
	local Output = newKill - oldKill
	killGui.KillText.Visible = true
	if newKill > oldKill and Output == 1 and not Running then
		killGui.KillText.Text = "+"..Output.." kill!"
	elseif newKill > oldKill and Output == 1 and Running then
		numberIncrease += 1
		Output += numberIncrease
		killGui.KillText.Text = "+"..Output.." kills!"
	end
oldKill = newKill
	ticking = tick()
	Running = true
	repeat
		task.wait(1)
	until tick() - ticking >= endtick
	Running = false
	numberIncrease = 0
	newKill = 0
end)

basic version of my code, i cant be bothered to explain the fix i smelled too much gasoline im gonna pass out

Very likely.

OnlyTwentyCharacters

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