Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Checking Player Slots?

Asked by 10 years ago

So i've been trying to script a slot system to check if a person has a weapon or in this case, flashlight and or deagle?

I've tried the

while true do

but this resulted in a crash. Odd?

--Variables--
flashlight = game.Players.LocalPlayer.Backpack:FindFirstChild("Flashlight")
deagle = game.Players.LocalPlayer.Backpack:FindFirstChild("Deagle")
slot1 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot1
slot2 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot2

--Functions--
if flashlight then
    print("Player has a Flashlight!")
    slot1.Text = "Flashlight"
elseif flashlight == nil then
    print("No Flashlight Found!")
    slot2.Text = "Empty Slot"
if deagle then
    print("Player has a Deagle!")
    slot2.Text = "Deagle"
elseif deagle == nil then
    print("No Deagle Found!")
    slot2.Text = "Empty Slot"
    end
end

So what im trying to do is make the script keep re-checking the player's slots and see if they sitll have the item without lagging the server?

So if anyone could help me? That be great.

Thanks in advanced?

NEW ISSUE

With the help of you guys, it seems to work to a point,

--Variables--
flashlight = game.Players.LocalPlayer.Backpack:FindFirstChild("Flashlight")
deagle = game.Players.LocalPlayer.Backpack:FindFirstChild("Deagle")
slot1 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot1
slot2 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot2

--Functions--
if flashlight and deagle then
    print("Player has a Flashlight!")
    slot1.Text = "Flashlight"
    print("Player has a Deagle!")
    slot2.Text = "Deagle"
while wait() do
    game.Players.LocalPlayer.Backpack:FindFirstChild("Flashlight")
    slot1.Text = "Empty Slot"
    game.Players.LocalPlayer.Backpack:FindFirstChild("Deagle")
    slot2.Text = "Empty Slot"
    end
end

Now the issue is, even though the player has the items, it will still return "Empty Slot"

2 answers

Log in to vote
-1
Answered by 10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try this.

--Variables--
slot1 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot1
slot2 = game.Players.LocalPlayer.PlayerGui.Backpack.Backpack.Slots.Slot2

--Functions--
game:GetService("RunService").RenderStepped(function() --this will run every 1/60th of a second
    local flashlight = game.Players.LocalPlayer.Backpack:FindFirstChild("Flashlight")
    local deagle = game.Players.LocalPlayer.Backpack:FindFirstChild("Deagle")
    if flashlight then -- they have a flashlight
        print("Player has a Flashlight!")
        slot1.Text = "Flashlight"
    else -- they don't have a flashlight
        print("No Flashlight Found!")
        slot2.Text = "Empty Slot"
    end
    if deagle then -- they have a deagle,
        print("Player has a Deagle!")
        slot2.Text = "Deagle"
    else -- they don't have a deagle
        print("No Deagle Found!")
        slot2.Text = "Empty Slot"
    end
end)
0
I tried an "and" statement for this to check their character for when it's equiped but it checks both....Anything i can do? TwoSlhue 5 — 10y
1
replace with "or" AmericanStripes 610 — 10y
0
Thanks! TwoSlhue 5 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

The reason while true do crashed you, is because that function runs extremely fast, and will crash any server you put it in, as true is always true, and it is trying to keep up.

Try replacing it with while wait() do

0
I've editted my post with a new issue. TwoSlhue 5 — 10y

Answer this question