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
5 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

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)
0
Whats the error?? ben0h555 417 — 5y
0
i dont know but it says i got one on line 7 3wdo 198 — 5y
0
can you show the output? TigerClaws454 48 — 5y
0
its on line 4 but on this it makes it say line 7 for me 3wdo 198 — 5y
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 — 5y

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 5 years ago
Edited 5 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:

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.

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

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