Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a buyable house script?

i am starting to understand coding so i tried to make a buyable house but i got an error

price = 500

  

script.Parent.ClickDetector.MouseClick:Connect(function(hit)

if player:FindFirstChild("leaderstats") then --I know the player has leaderstats

if player.leaderstats.Cash.Value <= price then

player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price

end

end

end)

5 comments

0

Whats the error??

0

i dont know but it says i got one on line 7

0

can you show the output?

0

its on line 4 but on this it makes it say line 7 for me

0

With a click detector, you can just use (player) you don't use (hit) then you can say something like print(player.Name) and it would print the player's name that clicked it.

2 answers

There is a pretty simple way to do this. Now as your question is

non-constructive

I like to help everyone!

First, we have to setup the leaderstats. This can be done with a simple script:

game.Players.PlayerAdded:Connect(function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Value = 500
		cash.Parnet = leaderstats
end)

Next, we have to setup the main script so the player can buy the house:

local detector = script.Parent -- This is an example of the part you will touch to check if the player has enough cash.
local price = 500
local owned = false -- This will check if the house is already owned.

detector.Touched:Connect(function(hit)
	if owned == false then -- If the house is not owned then the function will run.
    	local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- This line of code gets the player from the character. So all characters are models in workspace and whenever you touch something this function is gettings what part of the character touched it. For example: if your torso touches it then the "hit" will be torso. And hit.Parent would be your character. So if we want to get the player from the character we will need this "hit.Parent".
		if player and player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Cash") then -- If we find the cash and the leaderstats that should be inside of the player.
			local cash = player.leaderstats.Cash
				
			if cash.Value >= price then -- If their cash value is grater than or equal to the set price then this function will run:
				
				cash.Value = cash.Value - price -- We're taking 500 cash because that's the price. We can't just say cash.Value = - price
				owned = true
				print(player.Name.." now owns a house!") -- This should be something like "player owns a house!"
			end
		end	
	end		
end)

I hoped this helped! If some code doesn't work just let me know. And next time, try to be more constructive with your question.

I found the error where it sad hit after function hit needed to be changed to player

This discussion is preserved as it was. New answers, comments, and votes are closed.