How can I fix this script?

Hi, I am running out of ideas about making this script. So, I am making a story game where of course is a dialogsystem with NPC´s and random players. But theres a main character that is being choosen random from the players. Normaly if I use the random player dialog system the main character is being called with his real playername. But I want the main character to be called son all the time. And if theres more then 1 player the son should be skipped and it will take a diffrent player. Anyways my problem is that the son is still being called with his real playername or sometimes it doesnt even work.

I used an if statement before but that didnt work. Then I tried to use Pcall, I dont know if Pcall is the right choice because Pcall is normaly being used in datastores as I heard. Does anybody know how I can make this script work?

Oh and the script is supposed to detect how many players are in a game. If theres only one player
it cant skip the son and the player will just be choosen all the time and called son. But if theres more then 1 player and the son has ben choosen then its supposed to skip the son and pick a diffrent player. And if the son has not ben choosen at the first time it will just continue with the player that was choosen.

I am out of ideas with this script and I dont think I made this script well. Does anybody have some ideas or should I remake this script?

function SetRandomPlayerText(word)
    Message = word
    RandomPlayer = nil
    wait()
	RandomPlayer = Players:GetChildren()[math.random(1, #Players:GetChildren())]
	local succes, otherplayer = pcall(function()
		RandomPlayer.Name = game.Workspace.Son.SonName.Value

	end)
	
	local PlayerCount = #Players:GetChildren()
	
	if succes and PlayerCount == 1 then
		print("succes theres only 1 player")
		PlayerName.Value = "Son" -- Sets the PlayerName to a random player's name
		wait() --
		PlayerImage:FireAllClients(RandomPlayer) -- Sets the image to a random player's head
		for i = 1, #Message do
			Text.Value = string.sub(Message, 1, i)
			TypeSound() -- Plays the typing sound
			wait(0.04)
		end
		
	elseif succes and PlayerCount > 1 then
		print("succes but theres more than 1 player")
		RandomPlayer = Players:GetChildren()[math.random(1, #Players:GetChildren())]
		
	elseif otherplayer and PlayerCount > 1 then
		print("son has not been chosen")
		PlayerName.Value = RandomPlayer.Name -- Sets the PlayerName to a random player's name
		wait()
		PlayerImage:FireAllClients(RandomPlayer) -- Sets the image to a random player's head
		for i = 1, #Message do
			Text.Value = string.sub(Message, 1, i)
			TypeSound()
			wait(0.04)
		end
		
	
	end
end

if somebody doesnt understand something about the script please tell me.

Any help is appreciated!

I’m pretty sure you can’t set the name of player like you did here:

Screen Shot 2022-06-30 at 2.48.36 PM

I’m not sure what you were trying to do.

I heard that Pcall is like an if statement. I taught it works like this: It checks if something has happened like: RandomPlayer.Name = game.Workspace.Son.SonName.Value. If it is then it will succes but if its not RandomPlayer.Name = game.Workspace.Son.SonName.Value then it will do otherplayer. If thats wrong then I guess I first need to learn how Pcall works.

No. Pcall is not like an if statement.
Read Here and here to understand what pcalls are.

In this case, you dont have to use them.
We often use it when we work with DataStore, BadgeService, GroupService, etc… in short - any function that related to roblox web itself and can fail when the web is down.

2 Likes

I am using an if statement now

function SetRandomPlayerText(word)
    Message = word
    RandomPlayer = nil
    task.wait(1)
	RandomPlayer = Players:GetChildren()[math.random(1, #Players:GetChildren())]
	
	local PlayerCount = #Players:GetChildren()
	
	if RandomPlayer == game.Workspace.Son.SonName.Value  and PlayerCount == 1 then
		print("succes theres only 1 player")
		PlayerName.Value = "Son" -- Sets the PlayerName to son
		task.wait() --
		PlayerImage:FireAllClients(RandomPlayer) -- Sets the image to a random player's head
		for i = 1, #Message do
			Text.Value = string.sub(Message, 1, i)
			TypeSound() -- Plays the typing sound
			task.wait(0.04)
		end
		
	elseif RandomPlayer == game.Workspace.Son.SonName.Value and PlayerCount > 1 then
		print("succes but theres more than 1 player")
		SetRandomPlayerText()
		
	else
		print("son has not been chosen")
		PlayerName.Value = RandomPlayer.Name -- Sets the PlayerName to a random player's name
		task.wait()
		PlayerImage:FireAllClients(RandomPlayer) -- Sets the image to a random player's head
		for i = 1, #Message do
			Text.Value = string.sub(Message, 1, i)
			--TypeSound() -- Plays the typing sound
			task.wait(0.04)
		end
	end
end

but somehow its saying that the son has not ben choosen even if I am alone on the server. If I am alone on this server it cant choose something diffrent than the son. I already tried a second elseif statement but that wont work.

Do you have an idea to fix that?

1 Like