Gives player tool when value is true

Hello! Recently I tried to make some values inside of player’s model, it looks like this.
image

What I want is, when the “HasGun” value is set to true (its a boolvalue btw). The game will give you a gun.

Here is the script

game.Players.PlayerAdded:Connect(function(player)
	
	--Folders
	local new    = Instance.new
	local plrdata= new("Folder", player)
	local data   = new("Folder", plrdata)
	local perks  = new("Folder", plrdata)
	
	--Data Values
	local bc     = new("IntValue", data)
	local level  = new("IntValue", data)
	local kills  = new("IntValue", data)
	local streak = new("IntValue", data)
	--Perks Values
	local hasgun = new("BoolValue", perks)
	
	
	plrdata.Name = "PlayerData"
	--Data Properties
	data.Name   = "Data"
	perks.Name  = "Perks"
	bc.Name     = "BedCoin"
	level.Name  = "Level"
	kills.Name  = "TotalKills"
	streak.Name = "Killstreak"
	
	--Perks Properties
	hasgun.Name = "HasGun"
	
	
	if hasgun.Value == true then
		local gun = game.ReplicatedStorage.Gun
		local plrs= game:GetService("Players")
		gun.Parent = plrs:WaitForChild("Backpack")
	end
	
	
	--[TESTING] money giver
	while true do -- gives player coins every second (just for testing, not really useful for now)
		bc.Value = bc.Value + math.random(1, 10)
		wait(1)
	end
	
end)

There’s no error in the output, and it doesn’t give me the gun when “HasGun” value is set to true. I’m also new at developing games, so if I make a mistake from the script or the value or the parents, please reply! I appreciate your reply

HasGun.Changed:Connect(function()
     if HasGun.Value == true then
         --Do stuff
     end
end)
1 Like

You need to have something which detects when the bool datatype changes, then use your current check method.

hasgun.Changed:Connect(function()
end
1 Like

I changed it a little bit, but it still doesn’t give the gun

hasgun.Changed:Connect(function(player)
		if hasgun.Value == true then
			local gun = game.ReplicatedStorage.Gun
			local gunc= gun:Clone()
			gunc.Parent = player.Backpack
		end
	end)

can I know where is the wrong part?

Player isnt a parameter in the Changed event

1 Like

change this to

hasgun.Changed:Connect(function()
		if hasgun.Value == true then
print("hi")
if hasgun.Parent.Parent.Parent == player then
print("hi2")
			local gun = game.ReplicatedStorage.Gun
			local gunc= gun:Clone()

			gunc.Parent = player.Backpack
print("done")
		end
end
	end)

Since player is already a parameter in the PlayerAdded Event, you don’t have to get the player again

1 Like

owh sorry my bad, but it still doesn’t give me the gun even though the value is true?
image

the gun also not in the backpack folder
image

(I used textlabel gui to change the value)

I edited my code to debug, try it again with the code and tell me what it prints

1 Like

It doesn’t print anything
image

Put it in a new script
Add a remote event in Replicated storage and name it HasGunEvent
your current script (copy this too btw )

game.Players.PlayerAdded:Connect(function(player)
	
	--Folders
	local new    = Instance.new
	local plrdata= new("Folder", player)
	local data   = new("Folder", plrdata)
	local perks  = new("Folder", plrdata)
	
	--Data Values
	local bc     = new("IntValue", data)
	local level  = new("IntValue", data)
	local kills  = new("IntValue", data)
	local streak = new("IntValue", data)
	--Perks Values
	local hasgun = new("BoolValue", perks)
	
	
	plrdata.Name = "PlayerData"
	--Data Properties
	data.Name   = "Data"
	perks.Name  = "Perks"
	bc.Name     = "BedCoin"
	level.Name  = "Level"
	kills.Name  = "TotalKills"
	streak.Name = "Killstreak"
	
	--Perks Properties
	hasgun.Name = "HasGun"

	--[TESTING] money giver
	while true do -- gives player coins every second (just for testing, not really useful for now)
		bc.Value = bc.Value + math.random(1, 10)
		wait(1)
	end
	
end)

new LOCAL script(put it in starter player scripts)

wait(1.5)
hasgun = game.Players.LocalPlayer.PlayerData.Perks.HasGun
player = game.Players.LocalPlayer
hasgun.Changed:Connect(function()
		if hasgun.Value == true then
print("hi")
if hasgun.Parent.Parent.Parent == player then
print("hi2")
game.ReplicatedStorage.HasGunEvent:FireServer(player)
			
print("done")
		end
end
	end)

New Normal Script (in ServerScriptService)

game.ReplicatedStorage.HasGunEvent.OnServerEvent:Connect(function(player)
local gun = game.ReplicatedStorage.Gun
			local gunc= gun:Clone()

			gunc.Parent = player.Backpack
end)
1 Like

I don’t see the point of declearing plrs, as it is the service Players and not the actual player object, plus you already have the player parameter above.

	if hasgun.Value == true then
		local gun = game.ReplicatedStorage.Gun
		local plrs= game:GetService("Players")
		gun.Parent = plrs:WaitForChild("Backpack")
	end

I see you used changed, but if there is nothing changing the bool value it won’t run.

hasgun.Changed:Connect(function(player)
		if hasgun.Value == true then
			local gun = game.ReplicatedStorage.Gun
			local gunc= gun:Clone()
			gunc.Parent = player.Backpack
		end
	end)

Do this

	if hasgun.Value == true then
		local gun = game.ReplicatedStorage.Gun:Clone()
		gun.Parent = player:WaitForChild("Backpack")
	end

So yeah.

1 Like

The local script in StarterPlayerScripts prints an error
image

and this
image

show me your Player Explorer again

1 Like

image

Try again with the updated scripts, it should work

1 Like

You’re doing it wrong. Find the player.

Like this:

local hasGun = player.PlayerData.HasGun
if hasGun.Value == true then
print("Has Gun")
end
1 Like

It printed hi and hi2 but it also gives this error
image

should I change it into FireServer(player) ?

Try looking at my post. It keeps the script in one place. And is more likely to work.

1 Like

It works! it can give the gun now. Thank you for everyone that replied! I appreciate all of you!

1 Like