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

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

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

✓ Accepted answer

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!

4 comments

0

Thanx for your time but I only need to change value of 1 player

0

Maby u can change that one? so I can accept your answer

0

Edited!

0

Secconf one works like a charm m8! Big thanx!

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)

4 comments

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?

0

dw about it . the first script is just explaining smth, use the 2nd one

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?

0

wth the guy basically took me code o well ;-;

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