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)
There is a pretty simple way to do this. Now as your question is
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
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?