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

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

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

2 answers

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.

1 comment

0

didn't work

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
)

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