I was trying to fire an event but it gave me this error even though got the player from character. I cannot think of any solutions so can someone explain why it gave me this error and the solution?
task.spawn(function(char)
local plr = game.Players:GetPlayerFromCharacter(char)
GameWin:FireClient(plr)
end)
There’s more code but it is definitely irrelevant.
You should print first in order to explain it to yourself. I believe in you, that you can solve it on your own if you do.
However from the code I’m guessing char is equal to nil and so player is nil because task.spawn doesn’t pass any parameters into the anonymous function given.
local char = --Get char outside of task.spawn
task.spawn(function()
local plr = game.Players:GetPlayerFromCharacter(char)
print(typeof(plr), plr, char)
GameWin:FireClient(plr)
end)
Sorry for the late response
Also I changed the script a bit
local Darkness = workspace.Darkness
local Trigger = script.Parent
local Hinge = workspace.End.ExitDoor.Hinge
local GameWin = game.ReplicatedStorage.GameWin
local TS = game:GetService("TweenService")
local WinFrame = game.StarterGui.ScreenGui.WinFrame
local Alarm = workspace.Alarm
local SmilerScreeh = workspace.Darkness.SmilerScreech
local DoorBang = workspace.GameStartPart.DoorBreak
local SmilerLight = workspace.Darkness.SmilerLight
local tweeninfo2 = TweenInfo.new(0.1)
local ClosingTween = TS:Create(Hinge, tweeninfo2, {CFrame = Hinge.CFrame * CFrame.Angles(0, math.rad(0), 0)})
local function CheckMagnitude(plr)
print("Close!")
ClosingTween:Play()
DoorBang:Play()
SmilerLight.Brightness = 0
Alarm:Stop()
SmilerScreeh:Stop()
wait(3)
WinFrame.Visible = true
end
repeat
wait(0.1)
print("Still Waiting!")
until (Darkness.Position-Trigger.Position).Magnitude < 4
CheckMagnitude()
for _, player in pairs(game.Players:GetPlayers()) do
local WinFrame = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("WinFrame")
WinFrame.Visible = true
end