Why won't this buy new zone script work?

Here’s a server script that I put under a zone part in my workspace, I don’t get any errors whatsoever and I don’t really understand what’s wrong?

local part = script.Parent
local rs = game:GetService("ReplicatedStorage")

local amount = part:WaitForChild("Amount").Value

local buyBorder = rs:WaitForChild("ServerEvents"):WaitForChild("BorderEvents"):WaitForChild("BuyNewBorder")
local ErrorEvent = rs:WaitForChild("ClientEvents"):WaitForChild("Sounds"):WaitForChild("Error")

part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		
		local char = hit.Parent
		local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
		
		local PlayerCash = plr:WaitForChild("leaderstats"):WaitForChild("Cash 💰").Value
		local PlayerZone = plr:WaitForChild("leaderstats"):WaitForChild("Zone 🗺️").Value
		
		if PlayerZone < 2 then
			if PlayerCash >= 500 then
				PlayerCash -= amount
				PlayerZone += 1
				buyBorder:FireClient(plr)
			else
				ErrorEvent:FireClient(plr)
			end
		end
	end
end)

the buyborder event fires a local script under the same zone part, that should make the parts cancollide false and make its transparency 1, while the error event fires a local script under starterGui that makes an error sound play

It seems like the issue may be related to updating the player’s Cash and Zone values properly. In your script, you are modifying the local variables PlayerCash and PlayerZone directly, but you should be updating the actual player’s properties instead.

Here’s a updated version of your script that should fix the issue:

local part = script.Parent
local rs = game:GetService("ReplicatedStorage")

local amount = part:WaitForChild("Amount").Value

local buyBorder = rs:WaitForChild("ServerEvents"):WaitForChild("BorderEvents"):WaitForChild("BuyNewBorder")
local ErrorEvent = rs:WaitForChild("ClientEvents"):WaitForChild("Sounds"):WaitForChild("Error")

part.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        
        local char = hit.Parent
        local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
        
        local PlayerCash = plr:WaitForChild("leaderstats"):WaitForChild("Cash 💰")
        local PlayerZone = plr:WaitForChild("leaderstats"):WaitForChild("Zone 🗺️")
        
        if PlayerZone.Value < 2 then
            if PlayerCash.Value >= 500 then
                PlayerCash.Value -= amount
                PlayerZone.Value += 1
                buyBorder:FireClient(plr)
            else
                ErrorEvent:FireClient(plr)
            end
        end
    end
end)

In this version of the script, I’ve updated the references to PlayerCash and PlayerZone to point to the actual properties of the player, rather than just their values. Now, when you update the values, it should properly update the player’s Cash and Zone as well.

I hope this works, let me know if it does not work, and send over any error messages if you get any :slight_smile:

1 Like

Thanks you so much, I never knew about this before!

1 Like

No problem mate, Goodluck with your game :smile:!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.