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

How do i change value of players cash only if player have a key in hes inventory?

Asked by 6 years ago

Here is my script. It works like this but i don't want other players to be able to click and get money if they don't have a key.

key is called "Blue Master Key" and it is a tool that player holds in hes inventory.

function onClicked()
for i=1, 1 do
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player:FindFirstChild("Xp") then
            player.Cash.Cash.Value = player.Cash.Cash.Value + 2000
        end
    end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Hello, kexiGamer!

You have to use this script:

function onClicked()
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Backpack:FindFirstChild("Blue Master Key")  and player.Backpack:FindFirstChild("Blue Master Key"):IsA("Tool") then
        player.Cash.Cash.Value = player.Cash.Cash.Value + 2000
        end
        if game.Worspace[player.Name]:FindFirstChild("Blue Master Key") and player.Backpack:FindFirstChild("Blue Master Key"):IsA("Tool") then
            player.Cash.Cash.Value = player.Cash.Cash.Value + 2000
        end
    end
end)
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Attention: This script will change the money value of ALL players

This another one jsut changes the stats of who clicked!

function onClicked(Clicker)
        if Clicker.Backpack:FindFirstChild("Blue Master Key")  and Clicker.Backpack:FindFirstChild("Blue Master Key"):IsA("Tool") then
             Clicker.Cash.Cash.Value = Clicker.Cash.Cash.Value + 2000
        end
        if game.Workspace[Clicker.Name]:FindFirstChild("Blue Master Key") and Clicker.Backpack:FindFirstChild("Blue Master Key"):IsA("Tool") then
             Clicker.Cash.Cash.Value = Clicker.Cash.Cash.Value + 2000
        end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)


Good Luck with your games!

0
Thanx for your time but I only need to change value of 1 player kexiGamer 21 — 6y
0
Maby u can change that one? so I can accept your answer kexiGamer 21 — 6y
0
Edited! Leamir 3138 — 6y
0
Secconf one works like a charm m8! Big thanx! kexiGamer 21 — 6y
Ad
Log in to vote
0
Answered by
iddash 45
6 years ago
Edited 6 years ago

Ok so, since you want to see if they have the key in their backpack, were are gonna use the argument that is passed when the MouseClick event is fired.

Here is what I mean :

part.ClickDetector.MouseClick:Connect(function(playerWhoClicked) --It gives us the player who clicked here
    print(playerWhoClicked)
end)

Since we can get the player like this, we can then access their backpack and see whether they have the key or not.

function onClicked(playerWhoClicked)
    local player = playerWhoClicked --Storing the player
    local backpack = player.Backpack -- Their Backpack
    if backpack:FindFirstChild('Blue Master Key') then -- looks for the key
        --Since its found the key, it can do the next bit
        if player:FindFirstChild("Xp") then 
                player.Cash.Cash.Value = player.Cash.Cash.Value + 2000
            end
    else
        print('No key found') --No key found
    end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
0
Hi there m8. Thanx for this one. I'm a compleete noob with scripts. How do I connect those 2 scripts? Do i make separate scripts or do i connect them somehow? kexiGamer 21 — 6y
0
dw about it . the first script is just explaining smth, use the 2nd one iddash 45 — 6y
0
Thanx m8! This script work only on pc when i deequip item with key4 on pc keyboard. It does not work on a smartphone or if i do mouseclick to unequip key on pc. Strange that it is like this but please can u help me solve this? kexiGamer 21 — 6y
0
wth the guy basically took me code o well ;-; iddash 45 — 6y

Answer this question