Why doesn't this prox work?

the script:

local prox = script.Parent

local canbuy = true

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.Triggered:Connect(function()
		if canbuy == true then
			player.leaderstats.fuel.Value = player.leaderstats.fuel.Value + -50
			canbuy = false
		else
			player.leaderstats.fuel.Value = player.leaderstats.fuel.Value + 0
		end
	end)
end)

what’s supposed to happen:
when a player trigger the ProximityPrompt the script will find the players leaderstats then find the money called “fuel” and if player has 50 fuel then they can walk through the door and lose -50 fuel.

If you can help with making it so when player buys wall and they leave game then when they join the game the wall is still bought and they don’t have to buy it again.

this game is supposed to be like a simulator with other players in a server and the wall is local to people who bought it or not.

1 Like

So, what’s the problem with your code here? You said what you’re trying to accomplish, but not what currently happens.

how much time do you have the hold duration set to cause if its 0 it wont work

for 1 second but it won’t function the script

the problem is that the player fuel won’t go down 50 and the wall won’t turn CanCollide false for that player.

maybe remove the plus sign in the eighth line

It will only run the code after a new player is added I believe, and it would be almost impossible to get it to work because instantaneously it will be done checking the script once. You should put it in a while wait loop or a infinite loop(i might be wrong).
If that does not work then remove the PlayerAdded

but the plus sign tells it to “add” the number value which in this case its minus and not addition. So it adding, minus 50.

thats not telling it to add -50 in arithmetics ± = - so its subtracting 50

so what should I add to the script then?

remove the plus sign if that doesnt work put the playeradded function in a while loop like min suggested

i think the problem is that it cannot find the player that triggered the prox.

Or maybe try this script

local canbuy
local prox = script.Parent


script.Parent.Triggered:Connect(function()
if [something] == 50=> then
	canbuy = true
end
	if canbuy == true then
			player.leaderstats.fuel.Value = player.leaderstats.fuel.Value + -50 -- the + and the -50 don't need to be removed because a plus and a negative make a negative.
			canbuy = false
		else
			player.leaderstats.fuel.Value = player.leaderstats.fuel.Value + 0
		end
	end)
end)

This code requires a understanding of math, for those saying to remove the + -50. He does not need to because don’t you remember in school a plus and a minus equals a negative. If the value of the fuel is greater than fifty, then 50 will not be accepted. You need to use =>, because that means 50 or greater.

here is the new code that i made:

local prox = script.Parent

local canbuy = true

while true do
	if prox.TriggerEnded then
		local player = prox.Triggered.Parent
		
		local leaderstats = player.leaderstats
		local fuel = leaderstats.fuel
		
		fuel.Value = fuel.Value + -50
		script.Parent.Parent.Parent.wall.CanCollide = false
		script.Parent.Parent.Parent.wall.Transparency = 1
	end
	if script.Parent.Parent.Parent.wall.CanCollide == false then
		break
	end
end

for some reason it still don’t work:

local canbuy
local prox = script.Parent

local wall = script.Parent.Parent.Parent.wall

canbuy = false

local players = game:GetService("Players")

local player = players.LocalPlayer

local fuel = player.leaderstats.fuel

if fuel.Value >= 50 then
	canbuy = true
end

script.Parent.Triggered:Connect(function()
	if canbuy == true then
		fuel.Value = fuel.Value + -50
		canbuy = false
	else
		fuel.Value = fuel.Value + 0
	end
end)

Let me see the output
put it in a while wait loop like this

while wait(1) do
if fuel.Value >= 50 then
	canbuy = true
    end
end

does it matter if it’s a local script or no?

Yes, put it in a local script please, if it is in a server script then everyone can access the wall if it is open.

its already in the local script when i started scripting it

You don’t need to make a playeradded function, triggered event returns the player that interacted with it.

script.Parent.Triggered:Connect(function(player)
    -- do fuel stuff
end)

Edit: Also do this fuel removing on the server then fire the client with a remote event and delete wall or make CanCollide false.

1 Like