Help with script

local BuyPart = script.Parent
local Price = 65 
local Player = game:GetService("Players").LocalPlayer
local CantAffordText = game:GetService("StarterGui"):WaitForChild("Error"):FindFirstChild("TextLabel")
local CoinsValue = Player:WaitForChild("leaderstats").Coins.Value 
local Debouce = false

if Player.leaderstats.Coins.Value >= Price then
	BuyPart.BrickColor = BrickColor.new("Lime green")
else
	BuyPart.BrickColor = BrickColor.new("Really red")
end

BuyPart.Touched:Connect(function()
	if Player and Debouce == false then
		Debouce = true
		task.wait(3)
		Debouce = false
		if Player and Player.leaderstats.Coins.Value >= Price and Debouce == false then
			print("Player and can afford this")
			Player.leaderstats.Coins.Value -= Price
		elseif Player and Player.leaderstats.Coins.Value < Price and Debouce == false then
			print("Player but cant afford")
			CantAffordText.Text = "You can't afford this! You need "..(Price - CoinsValue).." more coins to afford this."
		end
	end
	
end)

can someone tell me why this script isnt working

could you tell us what is the problem?
is there a error in output?

no thats why i cant find the issue

so the player will touch the buypart then buy something right?

you should check if the part that touches the buypart is a characater

alr changed the player to character but it still doesnt work

I don’t know much about leaderstats, but it could be because you’re only changing the players stats on the client; instead, try firing a remote event to a server script and changing the stats there.

its not only about the stats but the print functions are not working

1 Like

I was remaking this script to debug it and realized that this line: local BuyPart = script.Parent would imply that this was a script inside of the workspace, which in and of itself isn’t a problem, but this line: local Player = game:GetService(“Players”).LocalPlayer tells me it’s also a local script, which you can’t really and shouldn’t do, as if you try, the script won’t run. Am I just missing something?

the script is a local script, the script is in a part which is a child of a model. the model is in replicatedstorage but when a part gets touched, the model clones to the workspace. that part already works its just the script doesnt work

I just did a quick test and put a LocalScript inside of a model and then cloned the model to workspace and no print came out of the Output.

There’s technically a little trick to get around the no LocalScripts in workspace. You can do it by creating a normal script and changing its RunContext to Client.

wait i think i know whats the problem, i tried printing a message if the part was touched in general but it didnt print

It’s because of the fact it’s a local script that’s a descendant of workspace, the only exception to this rule is local scripts inside the character.

so i should change it to a normal script and use a remote event?

That would probably be for the best, but you would have to change a couple things about the script for it to work.

yeah yeah fs. Thank you for your time and help

1 Like

Here’s a quick little thing I made, although I had to get rid of a couple things as they’re exclusively local:

local buyPart = script.Parent
local price = 65
local debouce = false

local function IsPlayer(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then
		return game.Players:FindFirstChild(hit.Parent.Name)
	else
		return false
	end
end

buyPart.Touched:Connect(function(hit)
	local player = IsPlayer(hit)
	
	if player then
		debouce = true
		task.wait(3)
		debouce = false
		if player and player.leaderstats.Coins.Value >= price and debouce == false then
			print("Player and can afford this")
			player.leaderstats.Coins.Value -= price
		end
	end
end)

This is just a quick thing I made and I don’t know if it actually works :smile:

alr i closed studio now, i will try it later. Thank you!

You should mark one of your or my comments as solved so that people don’t come to this post to try to help with an already solved topic, and I created a better version that allows the CantAfford text to be displayed:

-- Local script that goes inside of either StarterPlayerScripts or StarterGui (I recommend StarterGui)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local starterGui = player:WaitForChild("PlayerGui")
local errorGui = starterGui:WaitForChild("Error")
local cantAffordText = errorGui:FindFirstChild("TextLabel")

local cantAffordRemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent name here")

local function CantAfford(cost)
	cost = cost or math.huge -- fail safe incase Cost comes through as nil
	
	cantAffordText.Text = "You can't afford this! You need "..cost.." more coins to afford this."
	errorGui.Enabled = true
	task.wait(2)
	errorGui.Enabled = false
end

cantAffordRemoteEvent.OnClientEvent:Connect(CantAfford)

-- Normal script that goes inside of the BuyPart.

local buyPart = script.Parent
local price = 65
local debouce = false

local cantAffordRemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent name here")

local function IsPlayer(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then
		return game.Players:FindFirstChild(hit.Parent.Name)
	else
		return false
	end
end

buyPart.Touched:Connect(function(hit)
	local player = IsPlayer(hit)

	if player then
		debouce = true
		task.wait(3)
		debouce = false
		if player and player.leaderstats.Coins.Value >= price and debouce == false then
			player.leaderstats.Coins.Value -= price
		elseif player and debouce == false then
			cantAffordRemoteEvent:FireServer(player, (price - player.leaderstats.Coins.Value)) -- we add the player at the start to tell the server which client to send the signal to.
		end
	end
end)

You need to create a RemoteEvent and put it inside of ReplicatedStorage.

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