Issue with remote functions firing for all players

I’m trying to make a button sim game, the basic concept is you walk over a button and if you have enough money you will buy some multiplier which means your income increases. I’m having an issue with getting remote events to work properly. I’ve already tried the wiki, videos, forum posts and debugging it myself for a long time. The issue is when the player walks over the button if other players in the server have enough money they will also purchase the button without touching it.

this is the events inside of workspace (the remove events are inside of the script) is a script

this scripts inside of starter player scripts and is a local script
image

2 Likes

A few problems here. First of all, you are using some deprecated things.

up1.Touched:connect(onTouched)

should be

up1.Touched:Connect(onTouched)

As @TheCarbyneUniverse said, you should use checks to ensure that the object who touched the part is a Humanoid.

Also, why do you have hit as a parameter in onTouched() if you aren’t going to use it??

3 Likes

I believe that touch() functions aren’t required to go in a Local Script. You can check via a Server Script whether something touched a part and check if that object is part of the character and has a humanoid:

up1.Touched:Connect(function(touchingPart)

    if touchingPart.Parent:FindFirstChild("Humanoid") tehn
    
        --player touched, nothing else
        --rest of the code here

    end

end)

Edit: This is very basic, but if you want, you can optimize this further to reduce any margin of error.

3 Likes

Actually, the player client who fired the event is automatically sent:

RemoteEvent:FireServer()

Here’s a direct quote:

By default, the functions connected to OnServerEvent will be passed the player who fired the event as the first parameter. If any other arguments are provided in the FireServer function, they will also be included in OnServerEvent after the player argument.

2 Likes

My bad… it seems I need to read up on events. lol

2 Likes

Do you have player defined as game:GetService(“Players”) anywhere? If so, that is your problem

i dont, i’ve tried checking if the button is touched in a server script and i’m not getting any result just errors

What happens if you print out

print(player)

in your OnServerEvent. Does it print out only the name of the player who touched the button, or the name of everybody who has over 100 money?

Idk but I believe it only prints out each player that touches at the exact time the touch it rather than collecting all users that touch it then releasing data at once.

[Edit:] 1 at a time(name)
I think, I haven’t tried it but I will test it later

It’s more because if you say that any players in the server with over 100 money buys the button too, just from one player touching the brick, that sounds to me like the OnServerEvent is being fired by every player in the game

Edit: So if you try to print out the print(player) and you see the name of everybody with over 100 money, under your first if statement, then I’m pretty sure you can assume that

for me its printing just the player that touches it
image

Okay, so that “Player2” didn’t print out from Player1 touching the button, is what you’re saying?

nvm yeah when player1 walks over it will print out player2 aswell

Yeah, then you have something in your code somewhere that fires to the serverevent anytime one player touches the button. Can you provide more of your code?

By the way, that Touched function is in a local script, right?

I’m pretty sure you can’t FireServer() from a server script

local script – ---------------------------- Variables ----------------------------local up1 = - Pastebin.com

script – ---------------------- Multipliers -------------------------script.upgrade.OnS - Pastebin.com

You need to specify who touches else you are just firing an event without a player…

function onTouched(hit)
   print("Touched")
   workspace.Events.upgrade:FireServer(hit.Parent.Name, 'what ever you wanna pass on here')
end

[Edit:] hit.Parent.Name is the player’s name who touched

When you FireServer() it automatically passes along the Player object as an argument. So what he does should work fine in this case.

**Local Script:**

Remote:FireServer()

**Server Script:**

Remote.OnServerEvent:Connect(function(Player)
print(Player)
end)

This would work fine ^

ill test it out and get back to you thanks a lot for the support i’ve been trying to figure this out on my own for a while

I honestly don’t know why it would fire multiple times for all players, but one thing you could try is to set it up like this.

local Player = game.Players.LocalPlayer

function onTouched(hit)	
	print("Touched")
	workspace.Events.upgrade:FireServer(Player)
end
up1.Touched:connect(onTouched)

And then do this in your server script:

script.upgrade.OnServerEvent:Connect(function(player, plr)
print(player)
	if player.Name == plr.Name then
		if player.leaderstats.Money.Value >= 100 then
			print("enough money")
			player.leaderstats.Money.Value = player.leaderstats.Money.Value - 100
			player.leaderstats.Multiplier.Value = player.leaderstats.Multiplier.Value + 1*(player.leaderstats.Rebirth.Value + 1)
			print("bought 1")
		else 
			print("too poor")
		end
	end 
end)