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
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.
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.
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
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?
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)