How can I make a cash given on kill script? (That saves)

I think that might have been the problem… Let me just go and check

Did you enable the provided settings:

Game > Security > Enable Studio Access To API Services

1 Like

API Services yes, HTTP requests, no. Do I enable that?

1 Like

I have that on already.

1 Like

API services is all you need, HTTP you dont need!

1 Like

Change the PlayerAdded code in the first script you mentioned in the original post to this and tell me if you get any errors:

game.Players.PlayerAdded:Connect(function(player)
	-- Listen for character added events
    player:WaitForChild("leaderstats")
    loadedData = LoadData(player)
    player.leaderstats:WaitForChild(currencyName).Value = loadedData["Cash"] or 0
	player.CharacterAdded:Connect(function(character)
		-- Listen for player deaths
		character:WaitForChild("Humanoid").Died:Connect(function()
			OnPlayerDeath(character)
		end)
	end)
end)

Keep in mind this will NOT load your plrtools data, you can add that yourself since that isn’t on-topic.

EDIT: Replied to the wrong person, and forgot to include or 0.

loadedData = LoadData(player)

“loadedData” and “LoadData” are underlined with orange…

1 Like

Did you create a variable and require the LoadData module in your script?

2 Likes

Make sure that my script is directly referencing the LoadData function in your module by instantiating this variable:

local LoadData = require(CHANGETOLOADDATA).LoadData
2 Likes

By “CHANGETOLOADDATA” does that mean I put the location of the “LoadData” script there? Or did I misunderstand that?

1 Like

Yes, sorry should have indicated. Change that to the location of LoadData, for example, game.ServerScriptService.LoadData

2 Likes

Did I do it right? “loadedData” is still underlined with orange

local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local LoadData = require(game.ServerScriptService.ShopServer.ModuleScripts.LoadData)

local ShopServer = game.ServerScriptService:WaitForChild("ShopServer")
local SaveData = require(ShopServer.ModuleScripts:WaitForChild("SaveData"))

-- Function to give cash to the creator of the killed player and save data after each kill
local function OnPlayerDeath(character)
	local creatorTag = character.Humanoid:FindFirstChild("creator")
	if creatorTag and creatorTag.Value then
		local stats = creatorTag.Value:FindFirstChild("leaderstats")
		if stats then
			local cashStat = stats:FindFirstChild(currencyName.Value)
			if cashStat then
				cashStat.Value = cashStat.Value + 20 -- Reward the creator with 20 cash
				local player = game.Players:GetPlayerFromCharacter(character)
				if player then
					local success = SaveData(player) -- Save player data after each kill
					if success then
						print("Data saved successfully for player:", player.Name)
					end
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	-- Listen for character added events
	player:WaitForChild("leaderstats")
	loadedData = LoadData(player)
	player.leaderstats:WaitForChild(currencyName).Value = loadedData["Cash"] or 0
	player.CharacterAdded:Connect(function(character)
		-- Listen for player deaths
		character:WaitForChild("Humanoid").Died:Connect(function()
			OnPlayerDeath(character)
		end)
	end)
end)
local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local LoadData = require(game.ServerScriptService.ShopServer.ModuleScripts.LoadData)

local ShopServer = game.ServerScriptService:WaitForChild("ShopServer")
local SaveData = require(ShopServer.ModuleScripts:WaitForChild("SaveData"))

-- Function to give cash to the creator of the killed player and save data after each kill
local function OnPlayerDeath(character)
	local creatorTag = character.Humanoid:FindFirstChild("creator")
	if creatorTag and creatorTag.Value then
		local stats = creatorTag.Value:FindFirstChild("leaderstats")
		if stats then
			local cashStat = stats:FindFirstChild(currencyName.Value)
			if cashStat then
				cashStat.Value = cashStat.Value + 20 -- Reward the creator with 20 cash
				local player = game.Players:GetPlayerFromCharacter(character)
				if player then
					local success = SaveData(player) -- Save player data after each kill
					if success then
						print("Data saved successfully for player:", player.Name)
					end
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	-- Listen for character added events
	player:WaitForChild("leaderstats")
	loadedData = LoadData.LoadData(player)
	player.leaderstats:WaitForChild(currencyName).Value = loadedData["Cash"] or 0
	player.CharacterAdded:Connect(function(character)
		-- Listen for player deaths
		character:WaitForChild("Humanoid").Died:Connect(function()
			OnPlayerDeath(character)
		end)
	end)
end)
1 Like

still underlined with orange.

Maybe it will work regardless of that? I will check wait

If it doesn’t work, try hovering on the underlined text, it should show a description.

it says this

image

do I turn it into

local = LoadData.LoadData(player)?

Yes, just make it local loadedData = LoadData.LoadData(player)

1 Like

20:23:56.696 Workspace.CashPerKillScript:32: attempt to index function with 'LoadData' - Server - CashPerKillScript:32

And I think it also causes the cash to not be given to the player too?

Change it to this and tell me if there is an error

local loadedData = LoadData(player)
1 Like