Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make a buyable house script? [closed]

Asked by
3wdo 198
6 years ago

This question has been solved by the original poster.

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

01price = 500
02 
03   
04 
05script.Parent.ClickDetector.MouseClick:Connect(function(hit)
06 
07if player:FindFirstChild("leaderstats") then --I know the player has leaderstats
08 
09if player.leaderstats.Cash.Value <= price then
10 
11player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price
12 
13end
14 
15end
16 
17end)
0
Whats the error?? ben0h555 417 — 6y
0
i dont know but it says i got one on line 7 3wdo 198 — 6y
0
can you show the output? TigerClaws454 48 — 6y
0
its on line 4 but on this it makes it say line 7 for me 3wdo 198 — 6y
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. namespace25 594 — 6y

Closed as Not Constructive by DeceptiveCaster, namespace25, Fifkee, and popeeyy

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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:

01game.Players.PlayerAdded:Connect(function(player)
02        local leaderstats = Instance.new("Folder")
03        leaderstats.Name = "leaderstats"
04        leaderstats.Parent = player
05 
06        local cash = Instance.new("IntValue")
07        cash.Name = "Cash"
08        cash.Value = 500
09        cash.Parnet = leaderstats
10end)

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

01local detector = script.Parent -- This is an example of the part you will touch to check if the player has enough cash.
02local price = 500
03local owned = false -- This will check if the house is already owned.
04 
05detector.Touched:Connect(function(hit)
06    if owned == false then -- If the house is not owned then the function will run.
07        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".
08        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.
09            local cash = player.leaderstats.Cash
10                 
11            if cash.Value >= price then -- If their cash value is grater than or equal to the set price then this function will run:
12                 
13                cash.Value = cash.Value - price -- We're taking 500 cash because that's the price. We can't just say cash.Value = - price
14                owned = true
15                print(player.Name.." now owns a house!") -- This should be something like "player owns a house!"
16            end
17        end
18    end    
19end)

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.

Ad
Log in to vote
0
Answered by
3wdo 198
6 years ago

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