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

is it possible to convert coins to cash in a game when a brick is touched? [closed]

Asked by 4 years ago
Edited 4 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

this is the script

script.Parent.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.parent)
        if Player:WaitForChild("leaderstats"):WaitForChild("Potatoes").Value == 6 then
            local Player = game.Players.GetPlayerFromCharacter(hit.Parent)          
                Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 10
            end
        end)

i don't get any errors but somethings not right because the cash value isn't going up

Closed as Not Constructive by EzraNehemiah_TF2

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
ACHRONlX 255 Moderation Voter
4 years ago

Please use a code block next time.

You should check first look for the Touched event on the part.

local Part = workspace.PartNameHere;

Part.Touched:Connect(function(Hit)

end)

Hit is the part that touched the part, eg the Left Arm, or Right Leg etc.

We need to check if Hit is a descendant of the Player's Character, we can do this in many ways.


local Part = workspace.PartNameHere; Part.Touched:Connect(function(Hit) local Player = game.Players:GetPlayerFromCharacter(Hit.Parent); if Player then local Value = Player.leaderstats.Potatoes; if Value.Value >= 6 then Value.Parent.Cash.Value += 10; -- This will add 10 on. end end end)

If it was, then the Player will be a player, otherwise it will be nil, thats why we check.

0
didn't work Cats767_99 -68 — 4y
Ad
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago

Please use the code block next time, it's the 6th button.

I currently have no idea why Zethusimam's code didn't work, so I'll rewrite it.

(By the way, please, next time give us more information instead of just throwing us the code expecting us to know exactly what your problem is.)

Anyways, here's what I believe what you did wrong: You probably have more than 6 Potatoes and/or a non-player touched it causing it the error. Try this:

script.Parent.Touched:Connect(
    function(hit)
        local Player = game.Players:GetPlayerFromCharacter(hit.parent)
        if Player then -- detecting if player
            if Player:WaitForChild("leaderstats"):WaitForChild("Potatoes").Value >= 6 then -- if 6 or more
                local Player = game.Players.GetPlayerFromCharacter(hit.Parent)
                Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 10
            end
        end
    end
)