Proximity prompt issue - lua

basically after my math.random it will teleport you to a location and then after 20 seconds it will teleport you back. The proximity prompt that this script is in only fires this script once, i click the button to go to sleep and it does nothing, no error code no thing. On the other hand after the else statement i can click the button three separate times and then if i try clicking it a fourth time it no longer works, still with no error.
Is there anyway I can fix this?
Heres my code if helpful
game.Players.PlayerAdded:Connect(function(player)
script.Parent.Triggered:Connect(function(activated)
if activated then
print("activated")
if game.ReplicatedFirst.night.Value == true then
script.Parent.Enabled = false
local number = math.random(1, 2)
print(number)
if number == 1 then
local plr = game.Workspace:FindFirstChild(player.Name)
game.ReplicatedFirst.slep.Value = true
plr.Humanoid.HipHeight = -0.913
plr.HumanoidRootPart.Position = Vector3.new(327.227, 1.5, -348.899) + Vector3.new(0,10,0)
print("ho")
player.PlayerGui.slepgui.Enabled = false
wait(20)
game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position = Vector3.new(31.546, 20.793, 12.62) + Vector3.new(0,10,0)
script.Parent.Enabled = true
game.ReplicatedFirst.night.Value = false
game.ReplicatedFirst.slep.Value = false
game.Workspace["Hotel - undertale"].Playing = true
game.Lighting.TimeOfDay = 12
else
local plr = game.Workspace:FindFirstChild(player.Name)
player.PlayerGui.slepgui.Enabled = true
script.Parent.Enabled = false
game.ReplicatedFirst.slep.Value = true
script.Parent.ObjectText = "Go to bed, this will skip the night"
game.Workspace["SNORING - SOUND EFFECT (128 kbps)"].Playing = true
print("number one worked")
wait(12)
plr.HumanoidRootPart.Position = Vector3.new(31.546, 20.793, 12.62) + Vector3.new(0,10,0)
game.ReplicatedFirst.night.Value = false
game.ReplicatedFirst.slep.Value = false
game.Workspace["Hotel - undertale"].Playing = true
player.PlayerGui.slepgui.Enabled = false
script.Parent.Enabled = true
game.Lighting.TimeOfDay = 12
script.Parent.ObjectText = "Go to bed. Only avaliable at night"
print("Numver two worked")
end
end
end
end)
end)
-- 327.227, 1.5, -348.899

Related

Unable to assign property AnimationId. Content expected, got nil

I don't know what's happening, but i'm getting that error in output. Apparently every animations of the Anims table and normalcombo table exists, and the property too. I tested it in Commandbar but didn't work. I don't know how to fix it and don't know what's the problem. Please help me. Code below:
--Local Script
local Replicated = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Remote = Replicated.CombateEvent
local Player = game.Players.LocalPlayer
local Char = Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local istoolequipped = false
script.Parent.Equipped:Connect(function()
istoolequipped = true
end)
script.Parent.Unequipped:Connect(function()
istoolequipped = false
end)
local lastM1Time = 0
local lastM1End = 0
local combo = 1
local canAir = true
local Anims = {
"rbxassetid://12270296026",
"rbxassetid://12290262661",
"rbxassetid://12290234803",
}
local normalcombo = {
"rbxassetid://12303443278",
"rbxassetid://12303443278",
"rbxassetid://12303527113",
"rbxassetid://12303770582"
}
function hb(size, cframe, ignore, char)
local hitbox = Instance.new("Part", workspace)
hitbox.Size = size
hitbox.CFrame = cframe
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Transparency = .6
hitbox.Name = "hb"
hitbox.Material = Enum.Material.ForceField
hitbox.CanQuery = false
local connection
connection = hitbox.Touched:Connect(function()
connection:Disconnect()
end)
local lasttarget
for _, v in pairs(hitbox:GetTouchingParts()) do
if v.Parent:FindFirstChild("Humanoid") and table.find(ignore, v.Parent.Name) == nil then
if lasttarget then
if (lasttarget.Position - char.PrimaryPart.Position).Magnitude > (v.Position - char.PrimaryPart.Position).Magnitude then
lasttarget = v.Parent.PrimaryPart
end
else
lasttarget = v.Parent.PrimaryPart
end
end
end
hitbox:Destroy()
if lasttarget then
return lasttarget.Parent
else
return nil
end
end
UIS.InputBegan:Connect(function(input, istyping)
if istyping then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and tick() - lastM1Time > .3 and tick() - lastM1End > .7 and istoolequipped then
if tick() - lastM1Time > .7 then
combo = 0
end
lastM1Time = tick()
local animation = Instance.new("Animation", workspace.Animation)
local air = nil
if UIS:IsKeyDown("Space") and canAir == true and combo == 2 then
canAir = false
air = "Up"
animation.AnimationId = Anims[1]
elseif UIS:IsKeyDown("Space") and combo == 3 and not canAir then
air = "Down"
animation.AnimationId = Anims[2]
else
animation.AnimationId = normalcombo[combo]
end
local load = Humanoid:LoadAnimation(animation)
load:Play()
animation:Destroy()
local hitTarg = hb(Vector3.new(3,5,3), Char.PrimaryPart.CFrame * CFrame.new(0,0,-3), {Char}, Char)
local Info = {
["Target"] = hitTarg,
["Combo"] = combo,
["Character"] = Char,
["Air"] = air
}
Remote:FireServer("Combo", Info)
if combo == #normalcombo then
combo = 1
lastM1End = tick()
else
combo += 1
end
Humanoid.WalkSpeed = 0
task.wait(.4)
Humanoid.WalkSpeed = 16
end
end)
The error send me to this line: "animation.AnimationId = normalcombo[combo]"
The error is telling you that normalcombo[combo] is returning nil when it should be a content url. That means that combo isn't a value between 1 and #normalcombo (4).
The only place where that could be is here :
if tick() - lastM1Time > .7 then
combo = 0
end
Here you are resetting the combo counter, but you set it too low. Arrays in lua are 1-indexed. You need to set it to :
combo = 1

attempt to perform arithmetic (add) on string and number Error

when I touch the object, I want it to be deleted and come back after 2 seconds and add 1 coin. how can I do this?
local WheatAmount = script.Parent
local oyuncuParasi = "Coins"
duvar = false
function onTouch(vurus)
if duvar == false then
oyuncuParasi = oyuncuParasi +1;
duvar = true
vurus.Parent:Destroy()
WheatAmount.CanCollide = false
wait(2)
WheatAmount.CanCollide = true
duvar = false
end
end
script.Parent.Touched:connect(onTouch)
You are trying to add 1 to local oyuncuParasi = "Coins" which is currently a string...
Try this:
local WheatAmount = script.Parent
local oyuncuParasi = 0
duvar = false
function onTouch(vurus)
if duvar == false then
oyuncuParasi += 1; -- Adds a coin
duvar = true
WheatAmount.Transparent = 1 -- Set the object Transparent
WheatAmount.CanCollide = false
WheatAmount.CanTouch = false
wait(2)
WheatAmount.Transparent = 0 -- Set the object Not Transparent
WheatAmount.CanCollide = true
WheatAmount.CanTouch = true
duvar = false
end
end
script.Parent.Touched:connect(onTouch)
Note: Make sure you put this in a server sided script instead of local script in the Object.
Let me know if it worked and the results!!

Roblox Studio Problems with Coden the UI Elements, what need to to?

Good day,
i have the problem that i wana make my visit ui only visible when i am at spawn so at first i wanted to do it with checking the team but i dont found any solousion on how to check which player is in which and than disable/enable ui for them. So after this i made an bool inside of the ui named Loby and got it to work that it turns on / off based on where you are but the problem is that i dont get the ui working i tried this now for serveral hours but i just dont get it and i dont know why. Its only prints not in Loby else if im in Loby or in a round ;(, i hope someone can help me!, Good day
FyMa2618
local loby = script.Parent.Loby
while loby.Value == false do
print("Not In Lobby")
script.Parent.Enabled = false
wait(.1)
end
while loby.Value == true do
local ready = script.Parent.Loaded
print("Lobby is Active")
script.Parent.Enabled = true
wait(.1)
end
--its a local script in the same position as the Bool.
The round script(nedded for the bool)
local intermission = 25
local roundLength = 45
local inRound = game.ReplicatedStorage.InRound
local staus = game.ReplicatedStorage.Status
-- when round value is changed
inRound.Changed:Connect(function()
if inRound.Value == false then
for i, plr in pairs(game.Players:GetChildren()) do
local char = plr.Character
local humanRoot = char:WaitForChild("HumanoidRootPart")
humanRoot.CFrame = game.Workspace.lobbySpawn.CFrame
end
end
end)
-- changes the status
local function round()
while true do
inRound.Value = false
for i = intermission, 0, -1 do
staus.Value = "Game will start in "..i.." seconds"
wait(1)
end
inRound.Value = true
wait(3)
for i = roundLength, 0, -1 do
wait(1)
staus.Value = "Game will end in "..i.." seconds"
local playing = {}
for i, plr in pairs(game.Players:GetChildren()) do
if plr.Team.Name == "Playing" then
local b = game.StarterGui.SpectateGUI.Loby
b.Value = false
table.insert(playing, plr.Name)
print("inserted player")
end
end
if #playing == 0 then
staus.Value = "Everyone Has Died"
wait(3)
break
end
end
end
end
The Region 3 Code(also needed for the bool)
local RegionPart = game.Workspace.RegionPart
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local region = Region3.new(pos1, pos2)
--[[
local part = Instance.new("Part")
part.Anchored = true
part.Size = region.Size
part.Parent = game.Workspace
part.CanCollide = false
part.Transparency = 0.4
dont worry about this
]]--
while true do
wait()
local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
for i, part in pairs(partsInRegion) do
if part.Parent:FindFirstChild("Humanoid") ~= nil then
local char = part.Parent
local loby = game.StarterGui.SpectateGUI.Loby
loby.Value = true
end
end
end[Starter UI][1]

Why does Roblox & Roblox Studio both crash when running certain scripts with no error code?

So here's the deal, I'm working on a game that is similar to a murder mystery and this part of the system is to start the round and show the players their roles and then the map. Except it used to work and I didn't knowingly do anything to the scripts that would affect it so the game crashes when this part is carried out. All I know is it starts to display the role and often does, it also plays the sound and displays the correct description, however it crashes just after that and sometimes just before the role is revealed. I've attached the function that runs when the RoundStarter event is fired to the client.
The server doesn't crash but, all of the clients do but not all at the exact same moment in time; Previously when there has been errors, I've been able to identify an error message but none of the clients throw any clear issues.
In testing mode in Roblox Studio, they freeze just after the drum roll sound effect as the role is displayed, however no errors and the server seems to be acting normally. Near enough the same thing happens when ran in Roblox.
Clientside script
ReplicatedStorage.Players.Events.RoundStarter.OnClientEvent:Connect(function(Role,Map)
local player = game.Players.LocalPlayer
player.CameraMode = Enum.CameraMode.LockFirstPerson
script.Parent.RoundGui.RoundEnd.Visible = false
game.Players.LocalPlayer.NameDisplayDistance = 0
CURRENTROLE = Role
CURRENTMAP = Map
script.Parent.RoundGui.Enabled = true
script.Parent.RoundGui.RoundStarter.Visible = true
script.Parent.RoundGui.RoundStarter.Fader.Visible = true
script.Parent.RoundGui.RoundStarter.Fader.BackgroundTransparency = 0
script.Parent.RoundGui.RoundStarter.RoleDescription.Visible = false
script.Parent.RoundGui.RoundStarter.RoleName.Visible = false
script.Parent.RoundGui.RoundStarter.Text.Visible = true
local transp = 0
for i = 1,50,1 do
transp += 0.05
script.Parent.RoundGui.RoundStarter.Fader.BackgroundTransparency = transp
wait(.1)
end
script.Parent.RoundGui.RoundStarter.DR:Play()
script.Parent.RoundGui.RoundStarter.RoleName.Visible = true
script.Parent.RoundGui.RoundStarter.RoleName.Text = Role
script.Parent.RoundGui.RoundStarter.RoleDescription.Visible = true
if Role == "Civilian" then
script.Parent.RoundGui.RoundStarter.RoleDescription.Text = "Your Job is to help the Inspector find the shadow... Work to figure them out and stop them before it's too late! Don't tell anyone your Role!"
script.Parent.RoundGui.RoundStarter.RoleName.TextColor3 = Color3.fromRGB(0, 255, 17)
elseif Role == "Shadow" then
script.Parent.RoundGui.RoundStarter.RoleName.TextColor3 = Color3.fromRGB(255, 0, 0)
script.Parent.RoundGui.RoundStarter.RoleDescription.Text = "It's time... Infiltrate the crowd, gain their trust and kill the inspector to win! By killing another player, You can take on their form and blend in with the Civilians. More help will be on left of the screen. Don't tell anyone your Role!"
elseif Role == "Inspector" then
script.Parent.RoundGui.RoundStarter.RoleName.TextColor3 = Color3.fromRGB(0, 38, 255)
script.Parent.RoundGui.RoundStarter.RoleDescription.Text = "You are nominated to fight the Shadow and eliminate them. You must work with the Civilians to figure out who the Shadow is, if you die you lose. Don't tell anyone your Role!"
end
wait(10)
script.Parent.RoundGui.RoundStarterMap.Visible = true
script.Parent.RoundGui.RoundStarterMap.Fader.Visible = true
script.Parent.RoundGui.RoundStarterMap.MapDescription.Visible = false
script.Parent.RoundGui.RoundStarterMap.MapName.Visible = false
local transp = 0
for i = 1,50,1 do
transp += 0.05
script.Parent.RoundGui.RoundStarterMap.Fader.BackgroundTransparency = transp
wait(.1)
end
script.Parent.RoundGui.RoundStarterMap.MapName.Visible = true
script.Parent.RoundGui.RoundStarterMap.MapName.Text = Map
script.Parent.RoundGui.RoundStarter.Dun:Play()
if Map == "The Office" then
script.Parent.RoundGui.RoundStarterMap.MapDescription.Text = "A classic map based in an Office building, in a power cut, near midnight. With an outdoor and Indoor area to explore, will you survive?"
elseif Map =="The Village" then
script.Parent.RoundGui.RoundStarterMap.MapDescription.Text = "The original map based in a small villiage with numerous buildings and areas to explore."
end
script.Parent.RoundGui.RoundStarterMap.MapDescription.Visible = true
wait(10)
script.Parent.RoundGui.RoundEnd.Visible = false
script.Parent.RoundGui.RoundStarterMap.Visible = false
script.Parent.RoundGui.RoundStarter.Visible = false
end)
--This is the next function in the code that should be executed at a similar time to the above one (It should have an InMap value of true.
ReplicatedStorage.Players:WaitForChild(player.Name).InMap.Changed:Connect(function()
data = game.ReplicatedStorage.GetPlayerDataCS:InvokeServer()
if ReplicatedStorage.Players[player.Name].InMap.Value == false then
--StartGUI:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
player.CameraMode = Enum.CameraMode.Classic
--StartGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Chat,true)
script.Parent.LobbyGui.Enabled = true
script.Parent.RoundGui.Enabled = false
wait()
else
if CURRENTROLE == "Shadow" then
script.Parent.RoundGui.ShadowPanel.Visible = true
script.Parent.RoundGui.CivPanel.Visible = false
script.Parent.RoundGui.InsPanel.Visible = false
if data then
if data.EquippedAbility ~="None" and data.EquippedAbility ~="" then
script.Parent.RoundGui.ShadowPanel[data.EquippedAbility].Visible = true
end
end
elseif CURRENTROLE == "Inspector" then
script.Parent.RoundGui.ShadowPanel.Visible = false
script.Parent.RoundGui.CivPanel.Visible = false
script.Parent.RoundGui.InsPanel.Visible = true
else
script.Parent.RoundGui.ShadowPanel.Visible = false
script.Parent.RoundGui.CivPanel.Visible = true
script.Parent.RoundGui.InsPanel.Visible = false
end
--StartGUI:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
--StartGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Chat,true)
script.Parent.LobbyGui.Enabled = false
script.Parent.RoundGui.Enabled = true
wait()
end
end)
Here's also the script on the server that should be near the event.
local replicatedstor = game:GetService("ReplicatedStorage")
local playersinthemap = {}
local playersinserver = game.Players:GetPlayers()
local maplist = {"The Village"}
local TESTINGMODE = false
local goodwin = false
local COUNT = 0
local deadpool = {}
local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore("THE_MASQUERADE_TEST2")
function UpdateStatuses(Playerr,NewStatus)
local c = Playerr
if NewStatus == "Lobby" or NewStatus == "AFK" then
if game.ReplicatedStorage.Players[c.Name].Status.Value == "Shadow" then
script.RoundsInProgress.Value = false
goodwin = true
elseif game.ReplicatedStorage.Players[c.Name].Status.Value == "Inspector" then
script.RoundsInProgress.Value = false
goodwin = false
end
game.ReplicatedStorage.Players[c.Name].InMap.Value = false
if table.find(playersinthemap,c,1)~= nil then
local index = table.find(playersinthemap,c,1)
table.remove(playersinthemap,index)
end
end
game.ReplicatedStorage.Players[c.Name].Status.Value = NewStatus
if #playersinthemap <= 2 and script.RoundsInProgress.Value == true then
script.RoundsInProgress.Value = false
goodwin = false
end
end
game.ReplicatedStorage.Players.Events.SetStatus.OnServerEvent:Connect(function(Player,Status)
if Status ~= nil then
UpdateStatuses(Player,Status)
else
print ("Tried to change status of Player but the given status was nil.")
end
end)
while true do
while script.RoundsRunning.Value == true do
game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Intermission...","L",false)
--game.ReplicatedStorage.Game.Intermission:Fire(30)
--game.ReplicatedStorage.Game.Intermission.Event:Wait()
wait(35)
game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Title.Text = "Round starting soon..."
--game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Round starting soon...","L",true)
--game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Time.Text = ""
if not game.Workspace.Map then
playersinthemap = {}
local Mapfolder = Instance.new("Folder",game.Workspace)
Mapfolder.Name = "Map"
end
playersinserver = game.Players:GetPlayers()
wait(10)
playersinserver = game.Players:GetPlayers()
if #playersinserver >= 4 or (TESTINGMODE == true and #playersinserver >= 3) then
game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Title.Text = "Round loading..."
game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Loading the round now...","L",false)
playersinserver = game.Players:GetPlayers()
local chosenMapIndex = math.random(#maplist)
local chosenMapName = maplist[chosenMapIndex]
local chosenShadowIndex = math.random(#playersinserver)
local chosenShadow = playersinserver[chosenShadowIndex]
local chosenInspectorIndex = math.random(#playersinserver)
local chosenInspector = playersinserver[chosenInspectorIndex]
while chosenInspector == chosenShadow do
chosenInspectorIndex = math.random(#playersinserver)
chosenInspector = playersinserver[chosenInspectorIndex]
end
local playernumber = playersinserver
COUNT = 0
while COUNT ~= #playersinserver do
COUNT += 1
local playername = playersinserver[COUNT].Name
print (playersinserver)
print ("Player trying to send role data:"..playername)
if playersinserver[COUNT] == chosenShadow then
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Shadow",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Shadow"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Shadow")
elseif playersinserver[COUNT] == chosenInspector then
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Inspector",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Inspector"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Inspector")
else
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Civilian",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Civilian"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Civilian")
end
end
wait()
local MapInServerStorage = game.ServerStorage.Maps:FindFirstChild(chosenMapName)
local LoadedMap = MapInServerStorage:Clone()
LoadedMap.Parent = game.Workspace.Map
local spawns = {}
for i,c in pairs(LoadedMap:GetDescendants()) do
if c.Name == "Spawn" then
table.insert(spawns,1,c)
end
end
COUNT = 0
local i = COUNT
while COUNT ~= #playersinserver do
COUNT += 1
print ("Setting Character locations. Loop:"..COUNT)
local spawnlistindex = math.random(#spawns)
playersinserver[COUNT].Character:SetPrimaryPartCFrame(spawns[spawnlistindex].CFrame)
--playersinserver[COUNT].Character:SetPrimaryPartCFrame(LoadedMap.Spawns.Spawn.CFrame)
table.insert(playersinthemap,1,playersinserver[COUNT])
playersinserver[COUNT].NameDisplayDistance = 0
game.ReplicatedStorage.Players:FindFirstChild(playersinserver[COUNT].Name).InMap.Value = true
if playersinserver[COUNT] == chosenInspector then
local data = game.ReplicatedStorage.GetPlayerData:Invoke(chosenInspector)
Pistl = game.ServerStorage.Guns[data.EquippedSkin]:Clone()
Pistl.Parent = playersinserver[COUNT].Backpack
end
end
print ("Round starting...")
--This is then followed by the rest of the round system which should be working but I have cut it to try to shorten the amount of reading but can be supplied if needed. It is closed correctly.
I've tried to keep the code to a minimum however it could be anywhere and as there are no obvious script related error codes, I'm requesting some assistance in some way.
There are other things that may have caused it but this is becoming a real issue, especially since it used to work, and I've included all of the likely suspects but I'm happy to give anything else if these aren't incorrect in anyway.
If you wish to see what happens yourself, you'll need either 4 Roblox Accounts or 3 extra friends.
Here's the game on Roblox: The Masquerade
If anything else is needed, feel free to let me know and thanks for reading and any potential help. (Just so you know, I have done a lot of research on all of the functions and things I'm using but I can't find any issues, it just freezes & crashes).
The only error codes I've got are:
Start Process Exception...
Uploading crash analytics
Done uploading crash analytics
There are some random wait() statements without any arguments, which shouldn't be in your code anyway. This may very well hang up the server / client.
Also, you appear to have a few while loops which do not have any wait()'s. That's a pretty common problem which stalls the client executing the script. Try adding at least
wait(0.01) to the end of each while true loop that doesn't have it already.
There might be some occurences of this in the client as well (like that one lonely wait() with no parameters), but here's a modified version of your server script:
while true do
while script.RoundsRunning.Value == true do
game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Intermission...","L",false)
--game.ReplicatedStorage.Game.Intermission:Fire(30)
--game.ReplicatedStorage.Game.Intermission.Event:Wait()
wait(35)
game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Title.Text = "Round starting soon..."
--game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Round starting soon...","L",true)
--game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Time.Text = ""
if not game.Workspace.Map then
playersinthemap = {}
local Mapfolder = Instance.new("Folder",game.Workspace)
Mapfolder.Name = "Map"
end
playersinserver = game.Players:GetPlayers()
wait(10)
playersinserver = game.Players:GetPlayers()
if #playersinserver >= 4 or (TESTINGMODE == true and #playersinserver >= 3) then
game.Workspace.Lobby.DATABOARD.SurfaceGui.Frame.Title.Text = "Round loading..."
game.ReplicatedStorage.Players.Events.Notify:FireAllClients("Loading the round now...","L",false)
playersinserver = game.Players:GetPlayers()
local chosenMapIndex = math.random(#maplist)
local chosenMapName = maplist[chosenMapIndex]
local chosenShadowIndex = math.random(#playersinserver)
local chosenShadow = playersinserver[chosenShadowIndex]
local chosenInspectorIndex = math.random(#playersinserver)
local chosenInspector = playersinserver[chosenInspectorIndex]
while chosenInspector == chosenShadow do
chosenInspectorIndex = math.random(#playersinserver)
chosenInspector = playersinserver[chosenInspectorIndex]
end
local playernumber = playersinserver
COUNT = 0
while COUNT ~= #playersinserver do
COUNT += 1
local playername = playersinserver[COUNT].Name
print (playersinserver)
print ("Player trying to send role data:"..playername)
if playersinserver[COUNT] == chosenShadow then
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Shadow",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Shadow"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Shadow")
elseif playersinserver[COUNT] == chosenInspector then
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Inspector",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Inspector"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Inspector")
else
game.ReplicatedStorage.Players.Events.RoundStarter:FireClient(playersinserver[COUNT],"Civilian",chosenMapName)
game.ReplicatedStorage.Players[playersinserver[COUNT].Name].Status.Value = "Civilian"
--temp = game.ReplicatedStorage.ReadStatus:Invoke(playername,"Civilian")
end
end
-- wait() useless wait
local MapInServerStorage = game.ServerStorage.Maps:FindFirstChild(chosenMapName)
local LoadedMap = MapInServerStorage:Clone()
LoadedMap.Parent = game.Workspace.Map
local spawns = {}
for i,c in pairs(LoadedMap:GetDescendants()) do
if c.Name == "Spawn" then
table.insert(spawns,1,c)
end
end
COUNT = 0
local i = COUNT
while COUNT ~= #playersinserver do
COUNT += 1
print ("Setting Character locations. Loop:"..COUNT)
local spawnlistindex = math.random(#spawns)
playersinserver[COUNT].Character:SetPrimaryPartCFrame(spawns[spawnlistindex].CFrame)
--playersinserver[COUNT].Character:SetPrimaryPartCFrame(LoadedMap.Spawns.Spawn.CFrame)
table.insert(playersinthemap,1,playersinserver[COUNT])
playersinserver[COUNT].NameDisplayDistance = 0
game.ReplicatedStorage.Players:FindFirstChild(playersinserver[COUNT].Name).InMap.Value = true
if playersinserver[COUNT] == chosenInspector then
local data = game.ReplicatedStorage.GetPlayerData:Invoke(chosenInspector)
Pistl = game.ServerStorage.Guns[data.EquippedSkin]:Clone()
Pistl.Parent = playersinserver[COUNT].Backpack
end
wait(0.01)
end
wait(0.01)
end
These are just the rough steps, so implant the changes in your code the way you want them to be.

Function only works at losing condition and not winning condition

When players lose the game the gameFun function is called but for some reason when i switch alert(win) to gameFun and the alert(lose) in processAnswer to alert(win), it doesn't work. My aim is to ask users a question after they won or lost the game.
--ball collides with balloon
function ballCollision(e)
if (e.other.name == 'balloon') then
--ball collision codes
end
if(target.text == '0') then
alert('win')
end
end
function checkGameover()
print( "Check game over" )
for i = 1, cannonBalls.numChildren do
print( i )
if(tonumber(ballRemain.text) <= 0) then
gameFun()
end
end
end
function gameFun()
balloonText.isVisible = false
balloonTextt.isVisible = false
balloonTexttt.isVisible = false
questionText.isVisible = false
askUser = display.newText('Is the cannon hard to use?', display.contentCenterX, display.contentWidth / 4, native.systemFont, 20 )
askUser:setFillColor(135, 75, 44)
yesBtn = display.newImage("Yes.png",120,290)
noBtn = display.newImage("No.png",190,290)
yesBtn:addEventListener ('tap', ansQuestion)
noBtn:addEventListener ('tap', ansQuestion)
end
function ansQuestion(event)
if event.target==noBtn then
answer = answer+0
else
answer = answer+1
end
print('send mail')
sendMail()
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
yesBtn:removeEventListener ('tap', ansQuestion)
noBtn:removeEventListener ('tap', ansQuestion)
alert('lose')
end
function alert(state)
gameListeners('rmv')
local alert
if(state == 'win') then
alert = display.newImage('win.png')
else
alert = display.newImage('lose.png')
end
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
alert.anchorX = 0.5
alert.anchorX = 0.5
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})
local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
score:setFillColor(135, 75, 44)
end
Also when sendMail function is on, it gives me this
As far as i can tell, it keeps going back and forth between ansQuestion and alert(state). Is there a way to fix this?
When you call alert(lose) in ansQuestion(), Lua interprets lose as a variable, not a string. Since the variable does not exist, it is nil, so the second branch of the if (state == 'win') gets executed. It so happens that in the case of "lose", this is the branch you want, but it is just luck. When you do alert(win), win is also a variable so it is nil, so the same branch gets executed, and that time it is the wrong branch. Use
alert('lose')
and
alert('win')
as you did in ballCollision listener. Note that there are several lines of duplicate code in ansQuestion and alert.
In your alert function you passed the string value 'lose'. That's why it's always calling losing condition.
As per my knowledge, you need to call this function.
local answer = 0
function ansQuestion(event)
if event.target==noBtn then
answer = 0
else
answer = 1
end
sendMail()
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
yesBtn:removeEventListener ('tap', ansQuestion)
noBtn:removeEventListener ('tap', ansQuestion)
alert(answer)
end
function alert(state)
gameListeners('rmv')
local alert
if(state == 1) then
alert = display.newImage('win.png')
else
alert = display.newImage('lose.png')
end
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
alert.anchorX = 0.5
alert.anchorX = 0.5
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})
local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
score:setFillColor(135, 75, 44)
end

Resources