Function isn't getting called

  1. What do you want to achieve?
    To make an TextButton that sets your respawnlocation to a random part and then resets your character

  2. What is the issue?
    A function isn’t being called

  3. What solutions have you tried so far?
    I tryed looking on the devforum but found nothing

I have 3 scripts,
one of them is a localscript, one of them is a modulescript, and one of them is a script

the localscript one isn’t very important in this issue, it just does a transition and sets the cameratype to custom, its just not important in this issue right now

the modulescript:

local clickChecker = {}

-- Variables
local localScript = script.Parent
local spawns = game:GetService("Workspace").dn_baseplate.spawns:GetChildren() and game:GetService("Workspace").dn_bigbaseplate.spawns:GetChildren()
----------

function clicked(player, humanoid)
	print("clicked clickChecker function ran")
	local target = spawns[math.random(#spawns)]
	player.RespawnLocation = target
	player:LoadCharacter()
	local functionRun = true
	if functionRun then
		print("functionRun")
		while true do
			print("while true do looping")
			wait(0.25)
			if humanoid.Died then
				print("humanoid died")
				player.RespawnLocation = target
				player:LoadCharacter()
			end
		end
	end
end

return clickChecker

The script:

local localScript = game:GetService("StarterGui").Menu.MainFrame.Buttons.Play.TextButton.LocalScript
local clickChecker = require(localScript:WaitForChild("clickChecker"))
local starterSpawn = game:GetService("Workspace").lobby.StarterSpawn

function ifClicked()
	wait(2)
	clickChecker.clicked()
	print("clicked")
end

function playerAdded(player)
	print("playerAdded")
	player.RespawnLocation = starterSpawn
	player:LoadCharacter()
end


localScript.Parent.MouseButton1Click:Connect(ifClicked)
game.Players.PlayerAdded:Connect(playerAdded)

I get no errors/warnings in output, The only thing that prints in output is “playerAdded”

5 Likes

Perhaps you should wait until the Player character is added?

Something like this:

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
-- Here
end)
end)

Alright i’ll try
One sec alright?

Wait where should i put the block of code

Delete your playerAdded function and replace it with this instead.

function playerAdded(player)
	player.CharacterAdded:Connect(function()
	player.RespawnLocation = starterSpawn
	player:LoadCharacter()
end)

end

ok i’ll try replacing it and then playtesting the game

wait i just gotta make one more playtest to be sure

@lilmazen1234
Im sorry i didn’t say this BUT
i made a little lobby box,
and then after 2 seconds when you press the textbutton…
it sets your spawnlocation to a random spawnlocation and then reloads your character

but what happens instead
is that i dont spawn in the lobby box,
but i spawn on a spawnlocation right away,

I also putted an print() in the characteradded function and the print didnt print

This is probably your issue.

You need to put the name of the table and a period before the function name in module scripts so it actually adds it to the module’s table at the start.

Change the line to this:

function clickChecker.clicked(player, humanoid)
1 Like

alright im gonna try it tysm! :smile:

i’ll also remove the characteradded since that messed up the lobby box and the spawns

yep now that you mentioned it i realized the problem that it was function clicked instead of clickChecker.clicked(player, humanoid)

1 Like

Don’t forget to mark it as solution if it worked! I don’t want to sound rude or greedy, but it’s to help others so they do not mistake the post as unsolved.

It looks like the issue with your script might be related to the fact that you are trying to call the clicked function from the clickChecker module without passing in any arguments. Since the clicked function expects two arguments (player and humanoid), you will need to modify your code to pass in these arguments when calling the function.

Additionally, you may want to consider using a RemoteFunction instead of a module script to handle the button click, as this would allow you to pass in the player object directly as an argument.

Here’s an example of what the updated script could look like using a RemoteFunction:

local starterSpawn = game:GetService("Workspace").lobby.StarterSpawn
local spawns = game:GetService("Workspace").dn_baseplate.spawns:GetChildren() and game:GetService("Workspace").dn_bigbaseplate.spawns:GetChildren()

local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "SetRandomSpawn"
remoteFunction.Parent = game:GetService("ReplicatedStorage")

function onButtonClicked(player)
    local target = spawns[math.random(#spawns)]
    player.RespawnLocation = target
    player:LoadCharacter()
end

remoteFunction.OnServerInvoke = onButtonClicked

function onPlayerAdded(player)
    player.RespawnLocation = starterSpawn
    player:LoadCharacter()
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

In this updated script, we create a RemoteFunction called SetRandomSpawn that is invoked on the server when the button is clicked. The onButtonClicked function is called whenever the SetRandomSpawn function is invoked, and sets the player’s respawn location to a random spawn location before reloading their character.

We also define an onPlayerAdded function that sets the player’s initial respawn location to starterSpawn when they first join the game.

In your LocalScript, you can then call the SetRandomSpawn function on the server using something like:

local remoteFunction = game:GetService("ReplicatedStorage").SetRandomSpawn

local function onButtonClicked()
    remoteFunction:InvokeServer()
end

script.Parent.MouseButton1Click:Connect(onButtonClicked)

This would handle the button click on the client side and call the SetRandomSpawn function on the server to handle the actual respawning logic.

1 Like

All of this is 100% unnecessary, and the issue was fixed by adding a single word and period.

1 Like

yeah i was gonna say that everything else worked fine but i was still in the lobby box

i have no words.

1 Like

oh also quick question do i delete the module script?

Yes, you can delete the module script if you’re no longer using it in your updated script.

1 Like

IT WORKS!
thank you so much!

note: the reason i wanted to make an lobby box and then spawn the player is because im making an nextbots game but when a nextbot oofs you before you press the play button, stuff glitches but with this it is now fixed :smiley:

i also had a feeling it had to do something with remotefunctions or remoteevents (probably)

1 Like

i do have a problem… when i die, my spawnlocation is still the same resulting to my nextbots spawncamping and spawnkilling

tryed’ something simple but due to the lack of my lua skills it just got an error

To solve this problem, you will need to update the player’s respawn location each time they die. You can do this by connecting to the Player.CharacterRemoving event and setting the player’s RespawnLocation property to a random spawn location before the player respawns.

Here’s an example of what the updated script could look like:

local starterSpawn = game:GetService("Workspace").lobby.StarterSpawn
local spawns = game:GetService("Workspace").dn_baseplate.spawns:GetChildren() and game:GetService("Workspace").dn_bigbaseplate.spawns:GetChildren()

local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "SetRandomSpawn"
remoteFunction.Parent = game:GetService("ReplicatedStorage")

function onButtonClicked(player)
local target = spawns[math.random(#spawns)]
player.RespawnLocation = target
player:LoadCharacter()
end

remoteFunction.OnServerInvoke = onButtonClicked

function onPlayerAdded(player)
player.RespawnLocation = starterSpawn
player:LoadCharacter()
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

function onCharacterRemoving(character)
local player = game.Players:GetPlayerFromCharacter(character)
if player then
local target = spawns[math.random(#spawns)]
player.RespawnLocation = target
end
end

game.Players.CharacterRemoving:Connect(onCharacterRemoving)

In this updated script, we define an onCharacterRemoving function that is called whenever a player’s character is removed. If the player is found, we set their RespawnLocation property to a random spawn location before they respawn.

By updating the player’s respawn location each time they die, this should prevent the issue of bots spawn camping and spawn killing.

1 Like

error: CharacterRemoving is not a valid member of Players “Players” at line 31

probably because the character isnt in the service Players i guess?