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

trying to award badge, but Player is a nil value?

Asked by
corbenv 17
4 years ago

trying to give badge every time the countdown ends. Please ignore everything but lines 1, 2, 5, 6, and 12. That is the only part I'm having trouble with. It's in a normal script.

local badgeID = 2124476391
local bs = game:GetService("BadgeService")
local Knock = script.Parent.Knock
local a = script.Parent
local Player = game.Players.LocalPlayer
local userId = Player.userId


while true do
    wait(math.random(5,6))
    Knock:Play()
    bs:AwardBadge(Player.UserId, badgeID)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.9)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
    wait(.4)
    a.Material = "SmoothPlastic"
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.5)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
    wait(.5)
    a.Material = "SmoothPlastic"
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.4)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
end

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

LocalPlayer only exists in a LocalScript. You'll have to come up with a different way to fetch players.

Ad
Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
4 years ago
Edited 4 years ago

You cannot access LocalPlayer from a server script. You would have to use a LocalScript for this, however, this would mean that every change you are making from the LocalScript that is meant to occur for all players will only affect the player who's client the script is running on. To combat this, you could use a RemoteEvent and fire it from the LocalScript using the :FireServer() function of RemoteEvents.

For example:

remoteEvent:FireServer()

Then, you can receive the signal from the server script using the .OnServerEvent event of RemoteEvents.

For example:

remoteEvent.OnServerEvent:Connect(function()
    --code
end)

Also, to let the LocalScript know that the countdown has been completed once again, you can fire the RemoteEvent once more except this time from the server to the client rather than vice versa. This time though, you would use :FireClient() instead of :FireServer and .OnClientEvent instead of .OnServerEvent because you are sending the signal from the server to the client.

So, the full LocalScript would look something like this:

local player = game:GetService("Players").LocalPlayer
local bs = game:GetService("BadgeService")
local badgeID = 2124476391
local remoteEvent = script.Parent:WaitForChild("RemoteEvent") --replace this with the location and name of the RemoteEvent

remoteEvent:FireServer()

remoteEvent.OnClientEvent:Connect(function()
    bs:AwardBadge(player.UserId, badgeID)
    remoteEvent:FireServer()
end)

And the full server script would look something like this:

local remoteEvent = script.Parent:WaitForChild("RemoteEvent") --replace this with the location and name of the RemoteEvent

remoteEvent.OnServerEvent:Connect(function()
    wait(math.random(5,6))
    script.Parent.Knock:Play()
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.9)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
    wait(.4)
    a.Material = "SmoothPlastic"
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.5)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
    wait(.5)
    a.Material = "SmoothPlastic"
    wait(.05)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.5
    wait(.05)
    a.Material = "SmoothPlastic"
    script.Parent.SurfaceLight.Brightness = 0
    wait(1.4)
    a.Material = "Neon"
    script.Parent.SurfaceLight.Brightness = 0.3
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 0.6
    wait(.1)
    script.Parent.SurfaceLight.Brightness = 1
    remoteEvent:FireClient()
end)

Both the LocalScript and server script should be of the same parent and the RemoteEvent could be located in either the same place, ServerStorage, etc. as long as you specify the location when declaring the variable. If there are any problems or errors please let me know as I would be happy to help. Also, I'm sorry if there are any formatting errors as I typed this here rather than a separate text editor because I'm lazy.

Answer this question