Function only works at losing condition and not winning condition - lua

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

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

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]

Proximity prompt issue

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

"Workspace.RedPedastal.Script:17: attempt to index function with 'Transparency'" on Roblox script

I'm making a roblox script that will teleport the player and make a "Beam" animation, but when i activate the script by walking on the tele pad, i get this error
Workspace.RedPedastal.Script:17: attempt to index function with 'Transparency'
Code:
local Teleport = "BeachHill"
local Beam = workspace.Beam
function Beam() #Just here incase of need
for i = 0, 100, 1 do
Beam.Transparency = 1 - i/100
Beam.Size = Vector3.new(2048, 1.8 + i/10, 1.8 + i/10)
print(Beam.Transparency, Beam.Size)
end
end
function Touch (hit)
if script.Parent.Locked == false and script.Parent.Parent : FindFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:FindFirstChild(Teleport).Locked=true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.parent:moveTo(Pos.Position)
for i = 0, 100, 1 do
Beam.Transparency = Beam.Transparency - 0.01 #<- Error
Beam.Size = Vector3.new(2048, 1.8 + i/10, 1.8 + i/10)
print(Beam.Transparency, Beam.Size)
end
wait(1)
script.Parent.Locked = false
script.Parent.Parent:FindFirstChild(Teleport).Locked = false
end
end
script.Parent.Touched:connect(Touch)
Functions and variables in Lua live in the same namespace, so you can't have a function and a variable both called Beam in the same place.

How do touch functions trigger correctly when used in a scroll group (corona)?

Touch Functions in a Scroll Group
I am attempting to add touch event listeners to objects that are placed in a scroll group. The scroll part is working fine, but the "ended" part of the touch function never registers.
I have seen that others have had this problem, but their solutions never fixed my problem - I have not though seen any using tables. In my code below it implements the code that fixed other people's problems.
I have tried re-organizing everything in different orders but that did not seem to change anything.
local widget = require("widget")
local scrollView = widget.newScrollView
{
left = 100,
top = 200,
width = 900,
height = 500,
maskFile = "white.png"
scrollWidth = 2000,
scrollHeight = 400,
hideBackground = true,
friction = 0.9 --0 is slowest
}
scrollView.x = 0
scrollView.y = H/2 - scrollView.height/2
The scroll screen above works fine...
local page = {}
page[1] = {exists = true}
page[2] = {exists = true}
page[3] = {exists = true}
page[4] = {exists = true}
page[5] = {exists = true}
page[6] = {exists = true}
page[7] = {exists = true}
page[8] = {exists = true}
page[9] = {exists = false}
page[10] = {exists = false}
Above I created a table.
for i = 1,#page do
if page[i].exists == true then
page[i].img = display.newImage("images/"..myScene..i..".png") --makes the different page link images
else
page[i].img = display.newImage("images/coming.png")
end
page[i].img:setReferencePoint( display.CenterReferencePoint)
page[i].img.x = (W+(i*2*W))/9 - 2*W/8 + 50
page[i].img.y = scrollView.height/4+20
if i > #page/2 then
page[i].img.x = (W+((i-#page/2)*2*W))/9 - 2*W/8 + 50
page[i].img.y = page[i].img.y + scrollView.height/2-20
end
local img = page[i].img
I rename it as a local variable because touch functions do not like [ ]
scrollView:insert(img)
group:insert(scrollView)
function img:touch(e)
if e.phase == "began" then
display.getCurrentStage():setFocus( img )
img.isFocus = true
elseif e.phase == "moved" then
local dx = math.abs( e.x - e.xStart )
local dy = math.abs( e.y - e.yStart )
if dx > 10 or dy > 10 then
scrollView:takeFocus( event )
end
The above part is what supposedly fixes this problem for others..
It checks if the x- or y-transition is more than 10 put the focus to my scrollview
elseif e.phase == "ended" then --it never gets to this part...
if page[i].exists == true then
storyboard.gotoScene(myScene..i)
else
audio.play(soon)
end
end
end
img:addEventListener("touch")
end
In your img:touch(e) function, you need to add "return true" right before the last end ... like below. Also, you can remove the extra code in the function which is not necessary ( setFocus (img) and img.isFocus = true ). I've found that that code does not perform well on Android devices.
Use the code below instead and it should work.
function img:touch(e)
if e.phase == "began" then
-- do something here
elseif e.phase == "moved" then
local dx = math.abs( e.x - e.xStart )
local dy = math.abs( e.y - e.yStart )
if dx > 10 or dy > 10 then
scrollView:takeFocus( event )
end
elseif e.phase == "ended" then
if page[i].exists == true then
storyboard.gotoScene(myScene..i)
else
audio.play(soon)
end
end
return true
end

Resources