Script doe's not reset when victim dies [Solved!]

The script works 100% but when the victim of the minigame dies the minigame doesn’t reset even thought I scripted it to

Code:

local Players = game.Players:GetPlayers()
local Sounds = script:WaitForChild("Playtime Sounds")
local HitBox = script.Parent
local Playtimegui = game.StarterGui:WaitForChild("Playtime ui")
local Rope = Playtimegui:WaitForChild("Rope")
local minigameprogress = script:WaitForChild("minigameprogress")
local minigameprogresstext = Playtimegui.Frame:WaitForChild("minigameprogress")
local contentloader = game:GetService("ContentProvider")
local isplaytimeavailable = true
local Minigamecompleted = false

Sounds["Playtime's Theme"]:Play()

Ropeassets = {
"rbxassetid://17309020766",
"rbxassetid://17309020583",
"rbxassetid://17309020358",	
"rbxassetid://17309020210",
"rbxassetid://17309020023"
}

Playtimesoundsassets = {
	"rbxassetid://17311954085",
	"rbxassetid://17311953908",
	"rbxassetid://17311953569",
	"rbxassetid://17311953180",
	"rbxassetid://17311953378"
}

contentloader:PreloadAsync(Ropeassets)
contentloader:PreloadAsync(Playtimesoundsassets)

HitBox.Touched:Connect(function(part)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
	if plr then
		if plr:FindFirstChild("Student") and plr:FindFirstChild("isplaying") then
			if plr.isplaying.Value == false then
				plr.isplaying.Value = true
				isplaytimeavailable = false
				wait(0.2)
				plr.Character.Humanoid.WalkSpeed = 0
				plr.Character.Humanoid.JumpPower = 40
				Sounds["Playtime ready go!"]:Play()
				plr.PlayerGui["Playtime ui"].Enabled = true
				Sounds["Playtime ready go!"]:Play()
				wait(Sounds["Playtime ready go!"].TimeLength)
				minigame(plr)
			end
		else	
			print("Player either doesn't have Student or is alreaady playing with playtime!")
		end	
	end
end)

function minigame(plr)
	repeat	
	if plr.Character and plr.Student then
			repeat
			plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
			plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020766"
			wait(0.3)
			plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020583"
			wait(0.3)
			plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020358"
			wait(0.3)
			plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020210"
			wait(0.3)
			plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020023"
			wait(0.3)
			plr.PlayerGui["Playtime ui"].Rope.Image = ""
				if plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Jumping or plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				if minigameprogress == 0 then
					Sounds["Playtime 1"]:Play()
					minigameprogress = minigameprogress + 1
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "1"
				elseif minigameprogress == 1 then
					Sounds["Playtime 2"]:Play()
					minigameprogress = minigameprogress + 1
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "2"
				elseif minigameprogress == 2 then
					minigameprogress = minigameprogress + 1
					Sounds["Playtime 3"]:Play()
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "3"	
				elseif minigameprogress == 3 then
					minigameprogress = minigameprogress + 1
					Sounds["Playtime 4"]:Play()
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "4"
				elseif minigameprogress == 4 then
					minigameprogress = minigameprogress + 1
					Sounds["Playtime 5"]:Play()
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "5"
					Minigamecompleted = true
					plr.isplaying.Value = false
					plr.PlayerGui["Playtime ui"].Enabled = false
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
				end			
			else
				minigameprogress = 0
				plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"	
				Sounds["Playtime oops!"]:Play()
				wait(Sounds["Playtime oops!"].TimeLength)	
			end
	        until Minigamecompleted == true
			
	elseif plr.Character and not plr.Student then
		plr.PlayerGui["Playtime ui"].Enabled = false
		minigameprogress = 0
		plr.PlayerGui["Playtime ui"].Rope.Image = ""
		Minigamecompleted = false
		plr.isplaying.Value = false	
		wait(3)	
		break
	end
		
		
	until plr.isplaying.Value == false
	
	wait(3)
	
	minigameprogress = 0
	Minigamecompleted = false
	isplaytimeavailable = true
	
end

Also at line 41 when the player walkspeed is supposed to be set to 0 it doesn’t set
Also more than 1 player can play the minigame even though it is supposed to be one at a time

3 Likes

Where did you put the script?
30 words

1 Like

Inside the character model of the person who forces the player to play their minigame

1 Like

You there?
(30 works requirement)

1 Like

yeah so after player dies their character is removed and readded I believe.
Put it in serverscriptservice maybe

1 Like

I can’t put it in serverscriptservice because the way I wrote it it needs to be in the charactor of the person who forces the player to do the minigame

1 Like

Can you not edit it then?
30 words

1 Like

this is just a rework of the basic script to make it more neat and will give you errors. please follow some of these tips so its actually readable. and functionable

local Players = game.Players:GetPlayers()
local Sounds = script:WaitForChild("Playtime Sounds")
local HitBox = script.Parent
local Playtimegui = game.StarterGui:WaitForChild("Playtime ui")
local Rope = Playtimegui:WaitForChild("Rope")
local minigameprogress = script:WaitForChild("minigameprogress")
local minigameprogresstext = Playtimegui.Frame:WaitForChild("minigameprogress")
local contentloader = game:GetService("ContentProvider")
local isplaytimeavailable = true
local Minigamecompleted = false

Sounds["Playtime's Theme"]:Play()

Ropeassets = {
	"rbxassetid://17309020766",
	"rbxassetid://17309020583",
	"rbxassetid://17309020358",	
	"rbxassetid://17309020210",
	"rbxassetid://17309020023"
}

Playtimesoundsassets = {
	"rbxassetid://17311954085",
	"rbxassetid://17311953908",
	"rbxassetid://17311953569",
	"rbxassetid://17311953180",
	"rbxassetid://17311953378"
}

contentloader:PreloadAsync(Ropeassets)
contentloader:PreloadAsync(Playtimesoundsassets)

--Use guard statments like if not return end its easier to read
HitBox.Touched:Connect(function(part)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
	if not plr then return end
	
	--Check for character and humanoid
	local Character = plr.Character or plr.CharacterAdded:Wait()
	if not Character then warn("No character found") return end
	if not Character:WaitForChild("Humanoid") then warn("No humanoid found") return end
	
	if not plr:FindFirstChild("Student") then warn("Student not found") return end
	if not plr:FindFirstChild("isplaying") then warn("isplaying not found") return end
	
	if not plr.isplaying.Value then -- debounce isnt a guard clause
		plr.isplaying.Value = true
		isplaytimeavailable = false
		--use task.wait instead of wait
		task.wait(0.2)
		Character.Humanoid.WalkSpeed = 0
		Character.Humanoid.JumpPower = 40
		Sounds["Playtime ready go!"]:Play()
		
		--check for playergui
		if plr:WaitForChild("PlayerGui") then
			plr.PlayerGui["Playtime ui"].Enabled = true
		end
		
		Sounds["Playtime ready go!"]:Play()
		task.wait(Sounds["Playtime ready go!"].TimeLength)
		minigame(plr)
	end
end)

function minigame(plr)
	--repeat statements inside one another is a terrible idea so laggy
	--try to find another way if possible in coding generally you wanna avoid loops
	repeat	
		if plr.Character and plr.Student then
		
		repeat
			--Make funcs so you dont repeat code
			local function UpdateRopeImage(Image:string,DoText:boolean)
				if DoText then
					-- should use waitforchild to make sure frame and minigame progress load 
					--but im too lazy
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
				end
				plr.PlayerGui["Playtime ui"].Rope.Image = Image
				task.wait(0.3)
			end
			
			UpdateRopeImage("rbxassetid://17309020766",true)
			UpdateRopeImage("rbxassetid://17309020583")
			UpdateRopeImage("rbxassetid://17309020358")
			UpdateRopeImage("rbxassetid://17309020210")
			UpdateRopeImage("rbxassetid://17309020023")
			UpdateRopeImage("")
			
			if plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Jumping 
				or plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall 
			then
				minigameprogress.Value += 1
				Sounds["Playtime"..tostring(minigameprogress.Value)]:Play()
				plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text =  tostring(minigameprogress.Value )
				if minigameprogress.Value  == 4 then
					Minigamecompleted = true
					plr.isplaying.Value = false
					plr.PlayerGui["Playtime ui"].Enabled = false
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
				end			
			else
				minigameprogress.Value  = 0
				plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"	
				Sounds["Playtime oops!"]:Play()
				wait(Sounds["Playtime oops!"].TimeLength)	
			end
		until Minigamecompleted

		elseif plr.Character and not plr.Student then
			plr.PlayerGui["Playtime ui"].Enabled = false
			minigameprogress.Value = 0
			plr.PlayerGui["Playtime ui"].Rope.Image = ""
			Minigamecompleted = false
			plr.isplaying.Value = false	
			task.wait(3)	
			break
		end


	until not plr.isplaying.Value

	task.wait(3)

	minigameprogress.Value = 0
	Minigamecompleted = false
	isplaytimeavailable = true
end

i dont even see a part where u coded in if they die?
also this should be a server script theres always another way to code something. the way this is being coded in itself is a bad system. dont use loops use events, dont repeat urself use functions.

if i better understood what it was you’re trying to achieve id show you how id go about doing it.

if you’re trying to find if someone died use

its likely that the player dies and the character variable for the new character isnt being updated so it will say that character = nil because you dont reset the variable when a new character is added which you can use character.added:wait()

Humanoid.Died:Connect(function() 
--Humanoid died :p
end)

it would REALLY do you some good to learn about events how to use them, how to disconnect them etc

3 Likes

You were right about the it will give you errors part it gave me a error

Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript:94: attempt to perform arithmetic (add) on Instance and number - Server - PlaytimeScript:94
14:35:04.533 Stack Begin - Studio
14:35:04.534 Script ‘Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript’, Line 94 - function minigame - Studio - PlaytimeScript:94
14:35:04.534 Script ‘Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript’, Line 62 - Studio - PlaytimeScript:62
14:35:04.535 Stack End - Studio

I am new to scripting so my code will mostly be messy I will try to learn about events and functions because I know I need to use them to add custom gamemodes to my game, I also try to learn stuff about CollectionService to not repeat myself

2 Likes

the variable minigameprogress is an instance and you’re trying to add +1 to it. i just condensed ur code but that error was still in the other code

if minigameprogress = 4 then

if minigameprogress = 3 then

those will always be false because minigame progress isnt a number its an instance

The problems with the original script was that 1. 2 or more players could play the minigame even though it was supposed to be only one 2. if the player dies while playing the minigame it doesn’t reset and keeps going 3. the progress counter didn’t update 4. at line 41 of the original script where the player walkspeed was supposed to be at 0 it doesn’t update and thats all

1 Like

yes because the original script errors.

local minigameprogress = script:WaitForChild("minigameprogress") 
-- this is an instance NOT a number

if minigameprogress == 0 then -- checking if its a number but its not
	Sounds["Playtime 1"]:Play()
	minigameprogress = minigameprogress + 1  -- wont add 1 because its not a number
	plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "1"
elseif minigameprogress == 1 then -- checking if its a number but its not
	Sounds["Playtime 2"]:Play()
	minigameprogress = minigameprogress + 1 -- wont add 1 because its not a number
	plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "2"  not
elseif minigameprogress == 2 then -- checking if its a number but its
etc...


1 Like

Reading this part I’m surprised that IntValues still doesn’t count as a number

1 Like

you have to use .Value to get number from an intvalue

if plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Jumping 
	or plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall 
then
	minigameprogress.Value += 1 --.Value
	Sounds["Playtime"..tostring(minigameprogress)]:Play()
	plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text =  tostring(minigameprogress)
	if minigameprogress.Value == 4 then --.Value
		Minigamecompleted = true
		plr.isplaying.Value = false
		plr.PlayerGui["Playtime ui"].Enabled = false
		plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
	end			
else
	minigameprogress.Value = 0 --.Value
	plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"	
	Sounds["Playtime oops!"]:Play()
	wait(Sounds["Playtime oops!"].TimeLength)	
end

1 Like

i fixed the original script so that u can use it

Gave me another error

Playtime1 is not a valid member of Folder “Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript.Playtime Sounds” - Server - PlaytimeScript:95
14:57:22.614 Stack Begin - Studio
14:57:22.616 Script ‘Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript’, Line 95 - function minigame - Studio - PlaytimeScript:95
14:57:22.616 Script ‘Workspace.Unfinished_Playtime.Hitbox.PlaytimeScript’, Line 62 - Studio - PlaytimeScript:62
14:57:22.617 Stack End - Studio

1 Like

I figured why its erroring, the sound effect you tried to look for Playtime1 actually haves a space so its actually Playtime 1

1 Like

I fixed the error myself however the main problem is still here if the player whos playing the minigame dies the minigame still doesn’t reset thats like the only reason I’m not releasing the character right now

1 Like
local Players = game.Players:GetPlayers()
local Sounds = script:WaitForChild("Playtime Sounds")
local HitBox = script.Parent
local Playtimegui = game.StarterGui:WaitForChild("Playtime ui")
local Rope = Playtimegui:WaitForChild("Rope")
local minigameprogress = script:WaitForChild("minigameprogress").Value
local minigameprogresstext = Playtimegui.Frame:WaitForChild("minigameprogress")
local contentloader = game:GetService("ContentProvider")
local isplaytimeavailable = true
local Minigamecompleted = false

Sounds["Playtime's Theme"]:Play()

Ropeassets = {
"rbxassetid://17309020766",
"rbxassetid://17309020583",
"rbxassetid://17309020358",	
"rbxassetid://17309020210",
"rbxassetid://17309020023"
}

Playtimesoundsassets = {
	"rbxassetid://17311954085",
	"rbxassetid://17311953908",
	"rbxassetid://17311953569",
	"rbxassetid://17311953180",
	"rbxassetid://17311953378"
}

contentloader:PreloadAsync(Ropeassets)
contentloader:PreloadAsync(Playtimesoundsassets)

HitBox.Touched:Connect(function(part)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
	if plr and isplaytimeavailable then
		if plr:FindFirstChild("Student") and plr:FindFirstChild("isplaying") then
			if plr.isplaying.Value == false then
				plr.isplaying.Value = true
				isplaytimeavailable = false
				wait(0.2)
				if plr.Character and plr.Character:FindFirstChild("Humanoid") then
					plr.Character.Humanoid.WalkSpeed = 0
					plr.Character.Humanoid.JumpPower = 40
				end
				Sounds["Playtime ready go!"]:Play()
				plr.PlayerGui["Playtime ui"].Enabled = true
				wait(Sounds["Playtime ready go!"].TimeLength)
				minigame(plr)
			end
		else	
			print("Player either doesn't have Student or is already playing with playtime!")
		end	
	end
end)

function minigame(plr)
	repeat
		if plr.Character and plr.Student then
			repeat
				plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
				plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020766"
				wait(0.3)
				plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020583"
				wait(0.3)
				plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020358"
				wait(0.3)
				plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020210"
				wait(0.3)
				plr.PlayerGui["Playtime ui"].Rope.Image = "rbxassetid://17309020023"
				wait(0.3)
				plr.PlayerGui["Playtime ui"].Rope.Image = ""
				if plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Jumping or plr.Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
					if minigameprogress == 0 then
						Sounds["Playtime 1"]:Play()
						minigameprogress = minigameprogress + 1
						plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "1"
					elseif minigameprogress == 1 then
						Sounds["Playtime 2"]:Play()
						minigameprogress = minigameprogress + 1
						plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "2"
					elseif minigameprogress == 2 then
						minigameprogress = minigameprogress + 1
						Sounds["Playtime 3"]:Play()
						plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "3"
					elseif minigameprogress == 3 then
						minigameprogress = minigameprogress + 1
						Sounds["Playtime 4"]:Play()
						plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "4"
					elseif minigameprogress == 4 then
						minigameprogress = minigameprogress + 1
						Sounds["Playtime 5"]:Play()
						plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "5"
						Minigamecompleted = true
					end
				else
					minigameprogress = 0
					plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
					Sounds["Playtime oops!"]:Play()
					wait(Sounds["Playtime oops!"].TimeLength)
				end
			until Minigamecompleted == true
			
			if Minigamecompleted then
				plr.isplaying.Value = false
				plr.PlayerGui["Playtime ui"].Enabled = false
				plr.PlayerGui["Playtime ui"].Frame.minigameprogress.Text = "0"
				Minigamecompleted = false
				minigameprogress = 0
				isplaytimeavailable = true
			end
			
		elseif plr.Character and not plr.Student then
			plr.PlayerGui["Playtime ui"].Enabled = false
			minigameprogress = 0
			plr.PlayerGui["Playtime ui"].Rope.Image = ""
			Minigamecompleted = false
			plr.isplaying.Value = false
			wait(3)
			break
		end
	until plr.isplaying.Value == false
end
1 Like