Function Not Running After Being Enabled By Another Script

Currently trying to make a script that when it detects that a player has all 3 checks as false it kicks them from the game or if the player has one of the 3 checks it prompts them with a purchase to an item. The player isn’t being detected, errors like “attempt to index nil with kick” or the function not being ran at all are the main errors. Tried removing players.playeradded and just putting a normal function however that’s when the 2 errors above come in, and if I leave it as is the function doesn’t get triggered(the script is being enabled by another script). The goal is to make this script server sided, Any help would be appreciated.

Code:

local Players = game:GetService("Players")

while true do
	
	wait(5)
	print("ok")

	function Check(player)
		
		print("In Check Function")
	
	if CheckV == true then
		MarketplaceService:PromptPurchase(player, ITEM_ID)
			BindE:Fire()
			print("Read... \n Continuing")
	end
	
	if CheckV2 == true then
		MarketplaceService:PromptPurchase(player, ITEM_ID2)
	end
	
	if CheckV3 == true then
		MarketplaceService:PromptPurchase(player, ITEM_ID3)
		end
		
	if CheckV == false and CheckV2 == false and CheckV3 == false then
			print("Kick Detected")
		player:Kick("You Do Not Own Any Items From The Event")
	end
	
end

Players.PlayerAdded:Connect(Check)
	
end
5 Likes

You’re creating another event every 5 seconds, because your call to Connect is inside the loop, probably not what you want. I don’t see CheckV CheckV2 or CheckV3 defined anywhere.

2 Likes

My Bad I’ll Upload All The Variables

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local BindE = game.ServerStorage.Purchase
local BindE2 = game.ServerStorage.Purchase2
local BindE3 = game.ServerStorage.Purchase3

local CheckV = game.ServerStorage.Check1.Value
local CheckV2 = game.ServerStorage.Check2.Value
local CheckV3 = game.ServerStorage.Check3.Value

local Check = game.ServerStorage.Check1
local Check2 = game.ServerStorage.Check2
local Check3 = game.ServerStorage.Check3

local ITEM_ID = 2598774005 --offered item

local ASSET_ID = 9255011 --owned item
local ASSET_NAME = "Silverthorn Antlers" --owned item name

local ITEM_ID2 = 25987754005 --offered item

local ASSET_ID2 = 9255011 --owned item
local ASSET_NAME2 = "Silverthorn Antlers" --owned item name

local ITEM_ID3 = 25987748005 --offered item

local ASSET_ID3 = 9255011 --owned item
local ASSET_NAME3 = "Silverthorn Antlers" --owned item name

Ignore that the same asset id is used 3 times that was for testing

3 Likes
local CheckV = game.ServerStorage.Check1.Value

This will only check the Value once. If the Value changes, CheckV will not change. You need to move the access of Value to the point in time where you actually want to check it.

2 Likes

So I would also put that in the loop? and what about the function(player) error is there an alternative where I can prompt the purchase and not have to connect player to the function?

2 Likes

Yes. It should look like CheckV.Value == true etc

2 Likes

my last question is the kick player line won’t work because of the error "“attempt to index nil with kick”
is there a way around that or is it impossible to keep it all server sided

1 Like

Are you sure it still has that error after you fixed the issue with Connect? Multiple events doing the same thing would cause this error.

1 Like

It’s completely ignoring the existence of the function, the loop is working but the function is not running at all

The error is saying the local variable player is nil, which it shouldn’t be.

Can you replace the first lines of Check with this and tell me what it prints?

function Check(...)
	print(...)

I put it in just like that it said "ServerScriptService.ScriptCheck:56: attempt to index nil with ‘Kick’ - Server - ScriptCheck:56

There are plenty of mistakes in your code boo, you’re placing the check function inside the while true do loop, your code is missing MarketplaceService reference, undefined variables: CheckV, CheckV2, and CheckV3 are not defined, missing ITEM_ID and ITEM_ID2 variables, try this code and make sure to replace the placeholders (ITEM_ID , ITEM_ID2 , etc.) with the actual values you need. Also remember to define and assign values to CheckV , CheckV2 , and CheckV3 appropriately Xx :kiss: :ribbon:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ITEM_ID = ID
local ITEM_ID2 = ID

function Check(player)
	local CheckV = 
	local CheckV2 = 
	local CheckV3 = 

	if CheckV then
		MarketplaceService:PromptPurchase(player, ITEM_ID)
		BindE:Fire()
	end

	if CheckV2 then
		MarketplaceService:PromptPurchase(player, ITEM_ID2)
	end

	if CheckV3 then
		MarketplaceService:PromptPurchase(player, ITEM_ID3)
	end

	if not CheckV and not CheckV2 and not CheckV3 then
		player:Kick("You Do Not Own Any Items From The Event")
	end
end

Players.PlayerAdded:Connect(Check)

And it didn’t print anything? 123123123

All the variables

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local BindE = game.ServerStorage.Purchase
local BindE2 = game.ServerStorage.Purchase2
local BindE3 = game.ServerStorage.Purchase3

local CheckV = game.ServerStorage.Check1.Value
local CheckV2 = game.ServerStorage.Check2.Value
local CheckV3 = game.ServerStorage.Check3.Value

local Check = game.ServerStorage.Check1
local Check2 = game.ServerStorage.Check2
local Check3 = game.ServerStorage.Check3

local ITEM_ID = 2598774005 --offered item

local ASSET_ID = 9255011 --owned item
local ASSET_NAME = "Silverthorn Antlers" --owned item name

local ITEM_ID2 = 25987754005 --offered item

local ASSET_ID2 = 9255011 --owned item
local ASSET_NAME2 = "Silverthorn Antlers" --owned item name

local ITEM_ID3 = 25987748005 --offered item

local ASSET_ID3 = 9255011 --owned item
local ASSET_NAME3 = "Silverthorn Antlers" --owned item name

forgot to send them in the original post

it just gave me the error attempted to index nil with kick

Make sure to replace the placeholder values (ITEM_ID, ASSET_ID, etc.) with the actual values you need babes

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local BindE = game.ServerStorage.Purchase
local BindE2 = game.ServerStorage.Purchase2
local BindE3 = game.ServerStorage.Purchase3

local CheckV = game.ServerStorage.Check1.Value
local CheckV2 = game.ServerStorage.Check2.Value
local CheckV3 = game.ServerStorage.Check3.Value

local Check = game.ServerStorage.Check1
local Check2 = game.ServerStorage.Check2
local Check3 = game.ServerStorage.Check3

local ITEM_ID = 2598774005 

local ASSET_ID = 9255011
local ASSET_NAME = "Silverthorn Antlers" 

local ITEM_ID2 = 25987754005

local ASSET_ID2 = 9255011 
local ASSET_NAME2 = "Silverthorn Antlers" 

local ITEM_ID3 = 25987748005 

local ASSET_ID3 = 9255011 
local ASSET_NAME3 = "Silverthorn Antlers" 

function Check(player)
	print("In Check Function")

	if CheckV then
		MarketplaceService:PromptPurchase(player, ITEM_ID)
		BindE:Fire()
		print("Read... \n Continuing")
	end

	if CheckV2 then
		MarketplaceService:PromptPurchase(player, ITEM_ID2)
		BindE2:Fire()
		print("Read... \n Continuing")
	end

	if CheckV3 then
		MarketplaceService:PromptPurchase(player, ITEM_ID3)
		BindE3:Fire()
		print("Read... \n Continuing")
	end

	if not CheckV and not CheckV2 and not CheckV3 then
		print("Kick Detected")
		player:Kick("You Do Not Own Any Items From The Event")
	end
end

Players.PlayerAdded:Connect(Check)

Hmmm. Can you move the function outside the loop? All I can think of, your code should work.

those are temporary for testing, I put them all the same to see if it would prompt the purchase, the main error is the function sending back attempted to index nil with kick. For some reason it doesn’t recognize I’m a player

alright I’ll try outside the loop

didn’t work, got completely ignored, I tried removing the entire connect thing and just putting Check(Players), which shot back the same error of attempting to index nil with kick