Cash giving part not working

Right, I’m attempting to make a money system without DataStores since they’re down right now, ik this causes all money to be lost when the person leaves the game but it just needs to be 1 session. The idea is editing an int value (Cash) inside PlayerScripts to add X money to the current when they touch the part to supply cash. Line 13 is when the script breaks and returns attempted to index nil with Cash but if I fix this it still breaks on that line. It’s a script not a local script and won’t work with a local script.

debounce = false

function onTouched(Hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	print("Checkpoint 1")
	if debounce == false and plr then
		print("Checkpoint 2")
		debounce = true
		print("Checkpoint 3")
		local LPlayer = game.Players.LocalPlayer
		print("Checkpoint 3.25")
		print("Checkpoint 3.5")
		local Cash = plr:FindFirstChild("PlayerScripts").Cash
		print("Checkpoint 4")
		Cash.Value = Cash.Value + 10
		print("Checkpoint 5")
		debounce = false
		print("Checkpoint 6")
	end
end

script.Parent.Touched:connect(onTouched)
3 Likes

You used Lplayer to reference the LocalPlayer

since this is a server script, you cannot use LocalPlayer. Local player is only to be used in a client(local) script.

Ah, how can I access the value since the rest of the system works this way and would be a pain to re-write all of it to work.

I cannot rewrite the whole script for you. But I will say that I recommend using a folder inside the player and a numbervalue inside the folder. The folder does not have to be named “leaderstats” but just anything for storage.

Like this:

https://gyazo.com/12ff867c9906f26c4551cc3ab5260255

2 Likes

There are some flows with your code, as @Developer6786 mentioned you are trying to reference LocalPlayer inside a server script, which will always be nil. Although the main issue is that the server has no access to PlayerGui because it’s not replicating to the server, therefore it always thinks it’s nil. Instead try parenting Cash in player or player.leaderstats so it can be replicated and modified from the server.

1 Like