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

Help with BoolValues and :Changed()?

Asked by 10 years ago

The below script should check for a BoolValue called RoundTag in the ReplicatedStorage and it should check for when the tag changes. I have a main script which changes it to true when the games has started. When it is true though it dosent give the players their weapons. Please Help!

Script Location: game>StarterPack>LocalScript

local player = script.Parent.Parent

local serverstorage = game:GetService("ServerStorage")
local laser = serverstorage:WaitForChild("Classes"):WaitForChild("Laser")
local rocket = serverstorage:WaitForChild("Classes"):WaitForChild("Rocket")
local demo = serverstorage:WaitForChild("Classes"):WaitForChild("Demo")
local assault = serverstorage:WaitForChild("Classes"):WaitForChild("Assault")

game:GetService("ReplicatedStorage"):FindFirstChild("RoundTag").Changed:connect(function(value)
    if value == true then
        if player.Class.Value == "Laser" then
            player.StarterGear:ClearAllChildren()
            local weapon = laser.Phaser
            weapon:Clone().Parent = player.Backpack
            weapon:Clone().Parent = player.StarterGear
        elseif player.Class.Value == "Rocket" then
            player.StarterGear:ClearAllChildren()
            local weapon = rocket.Launcher
            weapon:Clone().Parent = player.Backpack
            weapon:Clone().Parent = player.StarterGear
        end
    end
end)

EDIT Works in Solo but not on server

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

You code looks pretty good, although there are a few things you could improve.

The proper method to get the Player from local scripts is game.Players.LocalPlayer.

There is no need to wait for Classes more than once. After line 04 you know it exists.

FindFirstChild, when used like this, is useless. FindFirstChild either returns the child or it returns nil. Its only advantage is that it's in method form, so it will not throw an error immediately if the child doesn't exist, it just returns nil. However, it is only useful when used in tandem with an if statement.

If FindFirstChild on line 09 returns nil, you are basically doing

nil.Changed:connect(function(value)

It's also possible that the value is changed before the Changed event is connected, which would cause the event not to fire. This is more likely in this case because this script won't run immediately when the game starts, it's dependent on a player.

0
thanks :D I got it working now +1 NinjoOnline 1146 — 10y
Ad

Answer this question