Help to remove the province from the enemy (leaderstats)

I’m making a game that looks vaguely like HoI4. So in the game you need to capture other people’s provinces and the leaderboard should show the exact number of provinces owned by the player.
When a player steps on a square (province), it is colored in his color and this cell is assigned a value with the player’s nickname.
I need to make sure that when the player attacks an already occupied province, the enemy has -1 province.
But for some reason it doesn’t work.
Here’s my script, thanks in advance.

local Players = game:GetService("Players")
script.Parent.Touched:connect(function(Hit)
	if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid")then
		local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if script.Parent.BrickColor ~= player.TeamColor and script.Parent.Player.Value ~= player.Character then
			player.leaderstats.Provinces.Value = player.leaderstats.Provinces.Value + 1
			script.Parent.BrickColor = player.TeamColor
			script.Parent.Player.Value = player.Character
			local enemyname = script.Parent.Player
			local enemy = Players:FindFirstChild(enemyname.Value)
			print(enemyname.Value)
			enemy.leaderstats.Provinces.Value = enemy.leaderstats.Provinces.Value - 1
		end
	end
end)



3 Likes

Can you check if there are by chance any output errors ?

1 Like

Why are you comparing a value and a Instance (script.Parent.Player.Value ~= player.Character)? I’m assuming you’re storing the Player’s username, so you should change this part to:

		if script.Parent.BrickColor ~= player.TeamColor and script.Parent.Player.Value ~= player.Character.Name then
1 Like

I stepped first in red and then in blue, and that’s what I got.

1 Like

1 Like

script cant find “leaderstats” folder.

player.leaderstats.Provinces.Value = player.leaderstats.Provinces.Value + 1

(rest of the code isnt working because of this error)
As far I know player is defined propely. Can you show me the code that creates leaderstats folder and provinces value ?

1 Like
game.Players.PlayerAdded:Connect(function(plr)
	local f = Instance.new("Folder", plr)
	f.Name = "leaderstats"
	local provs = Instance.new("IntValue", f)
	provs.Name = "Provinces"
	provs.Value = 0
end)
1 Like

I made mistake here (cant count lines lool)
Faulty line is
enemy.leaderstats.Provinces.Value = enemy.leaderstats.Provinces.Value - 1
Change this line

local enemyname = script.Parent.Player

into

local enemyname = script.Parent.Player.Value 

and everything should work

1 Like

sorry, I didn’t understand something, but right now my script looks like this and nothing is working.

local Players = game:GetService("Players")
script.Parent.Touched:connect(function(Hit)
	if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid")then
		local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if script.Parent.BrickColor ~= player.TeamColor and script.Parent.Player.Value ~= player.Character then
			player.leaderstats.Provinces.Value = player.leaderstats.Provinces.Value + 1
			script.Parent.BrickColor = player.TeamColor
			script.Parent.Player.Value = player.Character
			local enemyname = script.Parent.Player.Value
			local enemy = Players:FindFirstChild(enemyname)
			print(enemyname.Value)
			enemy.leaderstats.Provinces.Value = enemy.leaderstats.Provinces.Value - 1
		end
	end
end)
1 Like

now its shows It cant find leaderstats folder. You shared me leaderstats script before. Check if you didnt add/remove something in it by mistake during copying script here

1 Like

I kind of put that script back in place, but now the error I mentioned earlier has returned.

1 Like

I was talking about checking this script

1 Like
local Players = game:GetService("Players")
script.Parent.Touched:connect(function(Hit)
	if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid")then
		local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if script.Parent.BrickColor ~= player.TeamColor and script.Parent.Player.Value ~= player.Character then
			player.leaderstats.Provinces.Value = player.leaderstats.Provinces.Value + 1
			script.Parent.BrickColor = player.TeamColor
			script.Parent.Player.Value = player.Character
			local enemyname = script.Parent.Player.Value
			local enemy = Players:FindFirstChild(enemyname)
			print(enemyname)
			enemy.leaderstats.Provinces.Value = enemy.leaderstats.Provinces.Value - 1
		end
	end
end)
1 Like

then I deleted it by accident, I returned it, but another error remained.

1 Like

can you try changing only this line again ?

local enemyname = script.Parent.Player.Value
1 Like

I’ve already deleted it.Value and returned, but nothing has changed.

1 Like

I edited your script a bit and posted it as place. If you want me to writte changes I made let me know
testgame.rbxl (110,3 KB)

2 Likes