Rebirth function not working

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so when you click a part it gives you a rebirth and resets your time wasted using remote event
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that sometimes it only works for 1 player in the server or it doesn’t even work here is the script:
local plr = game.Players.LocalPlayer
game.ReplicatedStorage.RemoteEvents.RemoteEvent.OnClientEvent:Connect(function(Rebirths,Multiplied,IncreaseRebirthCostBy,Part)
	local Requirement = plr.ImportantFolder.RebirthCost.Value + Multiplied
	if plr.leaderstats.TimeWasted.Value >= Requirement then
		plr.leaderstats.Rebirths.Value += Rebirths
		plr.ImportantFolder.RebirthCost.Value += IncreaseRebirthCostBy
		plr.leaderstats.TimeWasted.Value -= Requirement
	end
	Part.Text = Requirement
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    It didn’t help.

Any help is appreciated!

2 Likes

.OnClientEvent and .OnServerEvent will always, have the first argument as player. Meaning your code should look like this:

game.ReplicatedStorage.RemoteEvents.RemoteEvent.OnClientEvent:Connect(function(plr, Rebirths,Multiplied,IncreaseRebirthCostBy,Part)
	local Requirement = plr.ImportantFolder.RebirthCost.Value + Multiplied
	if plr.leaderstats.TimeWasted.Value >= Requirement then
		plr.leaderstats.Rebirths.Value += Rebirths
		plr.ImportantFolder.RebirthCost.Value += IncreaseRebirthCostBy
		plr.leaderstats.TimeWasted.Value -= Requirement
	end
	Part.Text = Requirement
end)

And, is “Part” supposed to be a Surface/BillboardGui? Because Parts do not have a .Text property.

Oh yeah forgot to clarify that this is a local script and the remote event is getting fired from a script here is the script

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	game.ReplicatedStorage.RemoteEvents.RemoteEvent:FireClient(plr,1,1,250,script.Parent.SurfaceGui.Requirement)
end)

Oh, I see. But, there’s no need to send a Player when you fire a remote event, it will automatically have the first argument set to the player who fired it, meaning it should look like this:

script.Parent.ClickDetector.MouseClick:Connect(function()
	game.ReplicatedStorage.RemoteEvents.RemoteEvent:FireClient(1,1,250,script.Parent.SurfaceGui.Requirement)
end)

Tried your suggestion but it brings up an error saying

Unable to cast value to Object

Can you click it and see which line errors? Or you can’t just do that?

The error comes from this line:

game.ReplicatedStorage.RemoteEvents.RemoteEvent:FireClient(1,1,250,script.Parent.SurfaceGui.Requirement)

Not sure what’s the issue, are you sure it’s that line?

I meant the script with your suggestion in it since it works fine without your suggestion

  1. Did you try using my “suggestion”?
  2. Do you mean that my suggeston gave a error, or it was your own code?

I meant that your suggestion gave a error

Weird, since your code should give the error, because the first argument of the .OnClientEvent function is “Rebirths”, which would address the player, meaning this line:

plr.leaderstats.Rebirths.Value += Rebirths -- Tried to do arithmetic/Change a value of something with a Object.

Was supposed to error…

It doesn’t bring any errors when i try it

This is your issue. When using a remote event firing to a single client, it needs to know what client to fire

RemoteEvent:FireClient(playerthatisgettingfired,1,etc....

When recieving your event, the first parameter(player variable) is taken out.

RemoteEvent.OnClientEvent:Connect(function(1,etc...

Dev hub if you want to look at example scripts:https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

1 Like