Hello! Recently I tried to make some values inside of player’s model, it looks like this.
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
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)
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
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)
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