So I already have a shop where you can buy things, but I thought it would be interesting to unlock a sword when getting a certain amount of kills. I have looked around, and try to do it myself, but no success. I was hoping you guys can give me tips and suggestions on how to do this. Hope you have a wonderful day!
Hello, its pretty simple actually.
Firstly you want to go on the item GUI which you want to be able to unlock.
Go on the label where it is shown, and disable the script inside the buy button. (Or whatever your shop works like.)
Insert a local script inside the buy button for the specific item.
local kills = game.Players.LocalPlayer.leaderstats.Kills --We are finding the Kills in leadertstats.
local disabledScript = script.Parent --reference the 'Buy' script here.
while true do
wait(0.1) --This is to update the script every .1 seconds.
if kills.Value == 5 then --If the kills amount is equal to 5, then we fire a script.
disabledScript.Disabled = false --Enabling the script.
end
end
I hope that was explained well, if you need more explanation just reply to this.
Thanks.
If a script is disabled when the game starts then it will never run so the while loop won’t ever enable the script again however you can enable the script from an another script but this isn’t what @OP wants and the way your trying to guide him to do this is wrong and inefficient.
@OP you can simply store a weapon in ReplicatedStorage and then just check how many kills the player has in a script and if he does have the amount of kills necessary to get the next sword you can clone it from ReplicatedStorage and then parent it to the player’s Back and StarterGear.
The reason we parent it to the player’s Backpack and StarterGear is because if we parent it to the player’s Backpack he’s then going to lose it when he dies but if we parent it to the player’s StarterGear he will then be able to always spawn with that weapon and never lose it so we parent it to both so he will instantly have it in his Backpack and he will also have it in his StarterGear so he gets it back if he dies if you don’t want this then you can just parent it to the player’s Backpack.
If you want to force the player to equip the weapon then parent it to the player’s Character and that will do it.
Here’s an example code of this:
--\\Services//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
--\\Variables//
local ExampleWeapon = ReplicatedStorage:WaitForChild("ExampleWeapon")
--\\Code//
Players.PlayerAdded:Connect(function(plr)
if plr.leaderstats.Kills.Value >= 5 then
local WeaponClone1 = ExampleWeapon:Clone()
WeaponClone1.Name = "Light Sword"
WeaponClone1.Parent = plr.Backpack
local WeaponClone2 = ExampleWeapon:Clone()
WeaponClone2.Name = "Light Sword"
Weapon.Clone2.Parent = plr.StarterGear
end
end)
Hope this helped you, if you still have any questions then don’t hesitate to ask.
Where would I put this local script?
It’s not a local script if you put this in a local script it won’t work because of Filtering Enabled so what you need to do is put the code that I made above in a script in ServerScriptService and then edit it to fit your needs and then it should work perfectly, if you still have any questions then don’t hesitate to ask.
Ok thanks!
(30 charsssssssssss)
'''
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = stats
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Parent = stats
plr.CharacterAdded:connect(function(char)
local humanoid
repeat
humanoid = char:FindFirstChild("Humanoid")
wait()
until humanoid
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
local tag = humanoid:FindFirstChild("creator")
if tag then
local killer = tag.Value
if killer then
killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
if kills.Value == 5 then
game.StarterGui.ScreenGui.Shop.Sword1.Text = "Get now"
game.Workspace.Coin.Touched:Connect(function()
kills.Value = kills.Value +1
end)
end
end
end
end)
end)
end)
'''
Will your script work with this script?
Yes it should work, if you have any errors in the output then screenshot it or tell me about them and I will fix it for you.
Thank you so much for your help!
Wait for some reason it is not working because when I tested it, and I got 5 kills nothing happened
Is there any errors in the output?
If not then can you put print statements after each line to see where the script stops at?
when I tried in a 3 player server, in the output printed nothing, when I did single player, it only printed the first print statement
Could you show me the script where you put the print statement and tell me at what line the script stopped working?
'''
--\\Services//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
--\\Variables//
local Weapon = ReplicatedStorage:WaitForChild("AzureSword")
--\\Code//
Players.PlayerAdded:Connect(function(plr)
print("1")
if plr.leaderstats.Kills.Value >= 5 then
print("2")
local WeaponClone1 = Weapon:Clone()
print("3")
WeaponClone1.Name = "Light Sword"
print("4")
WeaponClone1.Parent = plr.Backpack
print("5")
local WeaponClone2 = Weapon:Clone()
print("6")
WeaponClone2.Name = "Light Sword"
print("7")
Weapon.Clone2.Parent = plr.StarterGear
print("8")
end
end)
'''
For single player only 1, and for multiplayer none
Try this code and tell me what happens but first make sure that AzureSword is a tool with a handle and the AzureSword tool is in ReplicatedStorage by having a part named “Handle” in it, if AzureSword doesn’t have a handle then uncheck the “RequiresHandle” property in the AzureSword tool, what I did in this new code is that I wrapped the if statements in the RunService’s Heartbeat function which is a loop that always runs the code inside it every heartbeat.
--\\Services//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
--\\Variables//
local Weapon = ReplicatedStorage:WaitForChild("AzureSword")
--\\Code//
Players.PlayerAdded:Connect(function(plr)
print("1")
RunService.Heartbeat:Connect(function()
if plr.leaderstats.Kills.Value >= 5 then
print("2")
local WeaponClone1 = Weapon:Clone()
print("3")
WeaponClone1.Name = "Light Sword"
print("4")
WeaponClone1.Parent = plr.Backpack
print("5")
local WeaponClone2 = Weapon:Clone()
print("6")
WeaponClone2.Name = "Light Sword"
print("7")
Weapon.Clone2.Parent = plr.StarterGear
print("8")
end
end
end)
The azure sword has a “Handle”
It works, but how would you do that with a sword shop, (When you reach level 5, you go to the shop and get it), and change the text from “Locked until level 5” to “Equip”, finally, the script spawns like 6 azure swords in your inventory.
You can just change the TextLabel’s Text property to “Equip” when you can equip the sword and for the 6 azure swords thing try this new code to fix it which will prevent it from happening and this also shows how u can change the text label to show “Equip”:
--\\Services//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
--\\Variables//
local Weapon = ReplicatedStorage:WaitForChild("AzureSword")
local GotAzure = false
--\\Code//
Players.PlayerAdded:Connect(function(plr)
print("1")
RunService.Heartbeat:Connect(function()
if plr.leaderstats.Kills.Value >= 5 and not GotAzure then
GotAzure = true
textlabel_location.Text = "Equip"
print("2")
local WeaponClone1 = Weapon:Clone()
print("3")
WeaponClone1.Name = "Light Sword"
print("4")
WeaponClone1.Parent = plr.Backpack
print("5")
local WeaponClone2 = Weapon:Clone()
print("6")
WeaponClone2.Name = "Light Sword"
print("7")
Weapon.Clone2.Parent = plr.StarterGear
print("8")
end
end
end)
That should fix all your problems, I think that your problem is now solved.
Would this script work also? (Parent of a button, in which you can get a sword.)
'''
if game.Players.LocalPlayer.leaderstats.Kills.Value >= 5 then
game.StarterGui.ScreenGui.Shop.Sword1.Text = "Equip"
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.GetSword:FireServer("AzureSword", script.Parent)
end)
end
'''
Yes it should work but that script should be a local script because :FireServer() can only be called from the client and then it will be picked up from a script with .OnServerEvent, also you need to give them the AzureSword in the server like inside of the .OnServerEvent function in a script not a local script.
Here’s a documentation about Remote Functions and Remote Events that will help you better understand them: Remote Functions and Events