Is Something Wrong with my script?

why is my script no working? no errors no warnings at all !!
what this script should do is if i press an image button the player loses 180 tokens but it doesn’t work, and idk how to make if the player purchases it the player can’t purchase it again
script:

local plr = game.Players.PlayerAdded:Wait()
local leaderstats = plr:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")

script.Parent.MouseButton1Click:Connect(function()
	if Tokens.Value >= 180 then
		Tokens.Value -= 180
		print("Purchase Successful By".. plr.Name)
	else
		print("Purchase Failed By".. plr.Name)
	end
end)

Try this instead:

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local Tokens = leaderstats:WaitForChild("Tokens")

	script.Parent.MouseButton1Click:Connect(function()
		if Tokens.Value >= 180 then
			Tokens.Value -= 180
			print("Purchase Successful By".. plr.Name)
		else
			print("Purchase Failed By".. plr.Name)
		end
	end)
end)

The issue is that I’m pretty sure game.Players.PlayerAdded:Wait() isn’t a good idea for getting the player, because it only works for one player. Use game.Players.PlayerAdded:Connect(function(Player) instead when getting multiple players.

1 Like

Is this running on a client-script or a server-script?

1 Like

you can use a boolean attribute to prevent purchasing it again
and test this one instead:

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local Tokens = leaderstats:WaitForChild("Tokens")

    script.Parent.MouseButton1Click:Connect(function()
        if Tokens.Value >= 180 then
            Tokens.Value = Tokens.Value - 180
            print("Purchase Successful By " .. plr.Name)
        else
            print("Purchase Failed By " .. plr.Name)
        end
    end)
end)
1 Like

client script necause it’s a button

the script doesn’t work
11111111

local plr = game.Players.LocalPlayer
wait(3) -- wait until character loads
local leaderstats = plr:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")

script.Parent.MouseButton1Click:Connect(function()
	if Tokens.Value >= 180 then
		Tokens.Value -= 180
		print("Purchase Successful By".. plr.Name)
	else
		print("Purchase Failed By".. plr.Name)
	end
end)
1 Like

Clicks should be handled via local script, but when checking it should be on the server.

Add a RemoteFunction named “PurchaseRequest” and put a server-script inside ServerScriptService. Code should be as followed.

game:GetService("ReplicatedStorage").PurchaseRequest.OnServerInvoke = function(Player, Price)
	local leaderstats = Player:WaitForChild("leaderstats")
	local Tokens = leaderstats:WaitForChild("Tokens") 

	if Tokens.Value >= Price then
		Tokens.Value = Tokens.Value - Price
		print("Purchase Successful By " .. Player.Name)
		
		return true
	else
		print("Purchase Failed By " .. Player.Name)
		
		return false
	end
end

And then inside your button, put this script. (Local Script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseRequest = ReplicatedStorage:WaitForChild("PurchaseRequest")

local Price = 180

script.Parent.MouseButton1Click:Connect(function()
	local RequestToPurchase = PurchaseRequest:InvokeServer(Price)
	
	if RequestToPurchase then
		print("True, bought")
	else
		print("False, not bought")
	end
end)

Try this.

thank you !
111111111111111111

sorry i couldn’t try yours :c
11111111

No I mean, you’re handling a purchase on the client. When you rejoin the tokens wont be saved nor be decreased.

oh i’ll choose yours and i’ll see

Alright, feel free to ask questions!

1 Like

uhhhh doesn’t work won’t it work without remote events ? it should work with datastores

Did you add a RemoteFunction? It should look like this
image

1 Like

OnServerInvoke can only be inplemented in server

image

Add a script inside serverscriptservice and add this code

game:GetService("ReplicatedStorage").PurchaseRequest.OnServerInvoke = function(Player, Price)
	local leaderstats = Player:WaitForChild("leaderstats")
	local Tokens = leaderstats:WaitForChild("Tokens") 

	if Tokens.Value >= Price then
		Tokens.Value = Tokens.Value - Price
		print("Purchase Successful By " .. Player.Name)
		
		return true
	else
		print("Purchase Failed By " .. Player.Name)
		
		return false
	end
end

im sorry but it doesn’t work
1111111

Let me check the error.

(charss)

hey thx for the try but i have tried the solution’s script and i added attribute in script so that it detects if the highlight is bought it works nicely and tysm for noticing the topic thank you for trying your best !!