So i’m trying to make a script that gives a player a gear if they have enough “time” but i’m bad at scriptin so it doesn’t work, here it is
local gravityCoil = game.ServerStorage.Gears.GravityCoil
local Time = player:FindFirstChild("Time")
if player.Time == 60< true then gravityCoil:Clone().Parent = player.Backpack
end
local gravityCoil = game.ServerStorage.Gears.GravityCoil
local Time = player:FindFirstChild("Time")
if player.Time >= 60 and not player.Backpack:FindFirstChild('GravityCoil') and not player.Character:FindFirstChild('GravityCoil') then gravityCoil:Clone().Parent = player.Backpack
end
All I did was add a check to see whether or not the player already has the gravity coil and if yes then it won’t give it again.
Instead of if player.Time == 60 < true write if Time.Value < 60
You already get the time variable on line 2, so you don’t need to do player.Time. You also need to check if time is below a certain value by using if num1 < num2, true is not a needed keyword in that phrase. Also, I am assuming you actually want Time.Value to be greater than or equal to 60, so what you most likely want is if Time.Value >= 60. Boolean expressions (comparisons that will be true or false already) don’t need to be compared to true. Nothing can be less than true.
Also, this script will only check the time once. You may want to do a while true do task.wait(1) end loop around it or connect it to a run service event, like Heartbeat. If you don’t know what that means, that’s ok, I would suggest checking this whenever the value is changed:
local gravityCoil = game.ServerStorage.Gears.GravityCoil
local Time = player:FindFirstChild("Time")
Time.Changed:Connect(function()
if Time.Value >=60 then
Time.Value = 0
gravityCoil:Clone().Parent = player.Backpack
end
end)
local gravityCoil = game.ServerStorage.Gears.GravityCoil
local Time = player:FindFirstChild("Time")
local db = false
while true do
wait()
if player.Time <= 60 and db == false then
db = true
local NewCoil = gravityCoil:Clone()
NewCoil.Parent = player.Backpack
end
end
LocalPlayer can’t exist in a server script, and a localscript couldn’t run in workspace OR be used to give a player a tool. The easiest solution for you would probably be to put the script in StarterPlayerScripts or StarterCharacterScripts depending on if you want this to happen each time they respawn or each time they join the game (the character scripts will be respawned if the player dies, for example). You can’t use LocalPlayer though, so you should access the player from where the script is.
You can read about these here: https://developer.roblox.com/en-us/api-reference/class/StarterPlayerScripts
or here: StarterCharacterScripts.
Sorry to bother but it doesn’t work and i’ve tried fixing it for a really long time and nothing is working. Here is everything about the script. It’s a server script located in the StarterPlayer StarterCharacterScripts. When I try running it the error, “FindFirstChild is not a valid member of RBXScriptSignal” shows up in the output. Here is the script itself: local gravityCoil = game.ServerStorage.Gears.GravityCoil
local player = game.Players.PlayerAdded
local Time = player:FindFirstChild(“Time”)
Time.Changed:Connect(function()
if Time.Value >=60 then
Time.Value = 0
gravityCoil:Clone().Parent = player.Backpack
end
end)
(Sorry I tried doing a lua code block but it’s not letting me)
Try creating a new server script inside ServerScriptService, and put this inside. It will create a time
value for the character when they spawn, and keep adding 1 time per second. When they reach 60 time, it will add the gear to the player. I’ve added comments so you know what each part is doing. Hope this helps.
local gravityCoil = game.ServerStorage:WaitForChild("Gears"):WaitForChild("GravityCoil")
function GivePlayerGear(gear, player)
gear:Clone().Parent = player.Backpack
end
game.Players.PlayerAdded:Connect(function(plr)
-- A player has joined the game.
plr.CharacterAdded:Connect(function(chr)
-- Their character has joined or respawned. Create a time value for them.
local time = Instance.new("NumberValue", chr)
time.Name = "Time"
spawn(function()
while chr ~= nil and chr.Parent ~= nil do
-- Keep incrementing time.
time.Value = time.Value + 1
wait(1)
end
end)
time.Changed:Connect(function()
if time.Value >= 60 and not plr.Backpack:FindFirstChild(gravityCoil.Name) and not chr:FindFirstChild(gravityCoil.Name) then
-- If the player has at least 60 time and doesn't already have the gear, give it to them.
GivePlayerGear(gravityCoil, plr)
end
end
end
end
PlayerAdded is an event, not a player. Here’s what you want to write:
local gravityCoil = game.ServerStorage.Gears.GravityCoil
game.Players.PlayerAdded:Connect(function(player)
local Time = player:FindFirstChild(“Time”)
Time.Changed:Connect(function()
if Time.Value >=60 then
Time.Value = 0
gravityCoil:Clone().Parent = player.Backpack
end
end)
end)
Don’t forget, you actually have to change the time value as well! You can do that by doing a while true do loop or something similar.
EDIT: this should be in ServerScriptService, not CharacterScripts since this connects to each player that joins
Hi, What you Could do is see if the Player has it or not
So Like this
local gravityCoil = game.ServerStorage.Gears.GravityCoil
local Time = player:FindFirstChild("Time")
local PlayerHasTool = Player.Backpack:FindFirstChild(gravityCoil.Name) or Player.Character:FindFirstChild(gravityCoil.Name)
if PlayerHasTool then
else
if player.Time == 60< true then gravityCoil:Clone().Parent = player.Backpack
end
end
(Sorry for the late reply!) It still doesn’t work and there’s no errors in the output. There was before but I fixed it and yet it still won’t work. Thanks for all the help so far by the way! I’m learning a lot from your help.
I don’t know if this will work and I haven’t read the replies but try this.
local gravityCoil = game.ServerStorage.Gears.GravityCoil --it could also be game.Players or something like that
game.Players.PlayerAdded:Connect(function(player)
local Time = player:FindFirstChild("Time")
if player.Time <60 then
gravityCoil:Clone().Parent = player.Backpack
elseif player.Time >=60 then
--Put here whatever you want it to do if they dont have enough time
end
end)
I think that is what you may be trying to do, also I hope the numbers are correct and that it works.
The problem is that it isn’t working yet it isn’t showing me what’s wrong in the output. The code makes sense and there aren’t any errors with it so I believe it’s not the script itself but where it is or something. It’s in ServerScriptStorage and it’s a server script.