Need help with some script

Hello everyone, welcome to this post.

I have trouble with a player getting kicked, if a value is greater than another value.

In this circumstance, the players value is greater than 0 but does not get kicked whilst joining (Kind of like a ban scheme.)

If anyone has any fixes, please lemme know.

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

	local Increment = player:WaitForChild("Stats"):FindFirstChild("Increment").Value
	
	print(Increment)
	
	if Increment >= 1 then
		
		player:Kick()
	else
		
	end
	
end)

3 Likes

What does print(Increment) print out?

Actually, there is a separate script, (if you want it you can tell me) where it increases the players amount so the next time a person joins they get auto kicked, but it seems as if data doesn’t save

See if this changes anything:

local PS = game:GetService("Players")

PS.PlayerAdded:Connect(function(player: Player)
	local stats = player:FindFirstChild("Stats")
	local increment = stats and stats:FindFirstChild("Increment")
	
	warn(increment.Value)
	if increment and increment.Value >= 1 then
		player:Kick()
	end
end)
1 Like

what is creating the increment value ? does it take time to be present in the player?

So, if the player talks it sends an remote event to the client, and then to the server back. (But there are decorations in client) Though the data is not updating for some reason?

No, unfortunately it doesn’t work

I think you can’t do “WaitForChild(”") and after that do FindFirstChild(""). Try doing both “WaitForChild(”")".

Well it’s not a problem with your script, but with your other script that increments the Increment value.

Ah, I’ll show you rn then.

Increment script:

local IncrementVal = game:GetService("ReplicatedStorage"):WaitForChild("IncrementVal")

function onListenEvent(player)
	
	local Increment = player:WaitForChild("Stats"):WaitForChild("Increment")
	
	Increment.Value += 1
	
	print(Increment.Value)
	
	
end

IncrementVal.OnServerEvent:Connect(onListenEvent)
1 Like

So if you say something in chat, it increments the value of Increment?

Yes. it does (filler wordhere_

The code that “Kicks” the player only run once, so it will never get kicked if you increment the value after the code that “Kicks” already runned.

1 Like

Do you use a Datastore to save Increment?

Yes, I did implement a datastore, which doesn’t seem to be working?

Could you show your Datastore script?

local Data = game:GetService("DataStoreService"):GetDataStore("statStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local s = Instance.new("Folder",player)
	s.Name = "Stats"
	
	local u = Instance.new("NumberValue",s)
	u.Value = 0
	u.Name = "Revivals"
	
	local i = Instance.new("NumberValue",s)
	i.Value = 0
	i.Name = "Increment"
	

	local dataload = Data:GetAsync(tostring(player.UserId))
	
	if dataload then
		
		u.Value = dataload[1] or 0
		i.Value = dataload[2] or 0
		
	end
	
	game.Players.PlayerAdded:Connect(function(player)

		Data:SetAsync(tostring(player.UserId),{

			player.Stats.Revivals.Value,
			player.Stats.Increment.Value

		})

	end)
	
end)

1 Like

What do you mean by all of that though? It increments the value so in the future it’ll kick the player.

If you are using a datastore, it will first run that code and after that load the datastore. The problem is that you are trying to kick a player with a value in the server that loads after the code “PlayerAdded” already runned, instead of using a value, Use “GetAsync()” to see if the datastore of the Player has change.

Change

if Increment >= 1

To

if Increment > 0
1 Like