Gui event firing table demoter Not working:(

  1. What do you want to achieve? Keep it simple and clear!
    i made a gui that if you input the user of someone it can fire an event and if the event if fired it checks the players rank who fired it and then compares it to the ingame target rank and if target’s rank is smaller than who fired it it changes the rank by -1 in the table to demote the target and then take 500 money to pay for it

  2. What is the issue? Include screenshots / videos if possible!
    i made the local values work but at the if statement it just doesen’t do anything and just stops or errors whatever i try in it
    heres the gui
    image
    heres the serverscript that handles it
    image


    heres the event
    image
    heres the localscript that fires it in the gui

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have tried alott of different variations of it like doing it with local or without or removing if statements and i looked through alll of the scripts many times if there are errors but now theres no errors in the console it just fails to do the action

heres the serverscript (currently)

local ranks = {"Player", "Associate", "Soldier", "Gang Leader", "Officer", "Senior Officer", "Chief Officer", "Commander", "Overseer", "Captain", "Major", "General", "Boss", "Right Hand Men", "Godfather"}

game:GetService("ReplicatedStorage"):WaitForChild("demote").OnServerEvent:Connect(function(user, target)
	local userrank = game:GetService("Players"):FindFirstChild(user.Name):WaitForChild("leaderstats", 5).Rank.Value
	local usermoney = game:GetService("Players"):FindFirstChild(user.Name):FindFirstChild("leaderstats").Money.Value
	local targetrank = game:GetService("Players"):FindFirstDescendant(target):FindFirstChild("leaderstats").Rank.Value
	local userrankindex = table.find(ranks, userrank)
	local targetrankindex = table.find(ranks, targetrank)
	if targetrankindex < userrankindex then do
			local indexfinal = targetrankindex - 1
			local rankfinal = ranks[indexfinal]
			targetrank = rankfinal
                        usermoney -= 500 --remove the cost
		end
	end
end)

heres the localscript

script.Parent.MouseButton1Click:Connect(function()
	wait(1)
	local target = script.Parent.Parent.user.Text
	if target ~= nil and game:GetService("Players").LocalPlayer:WaitForChild("leaderstats",5):FindFirstChild("Money").Value >= 500 then
		game:GetService("ReplicatedStorage").demote:FireServer(target)
	end
end)

so can someone help me with it i think its the tables functions that arent working properly but i looked through every variation of google search queries and couldnt find anything about it because for other it worked.

also if someone will comment tell me what im doing wrong because its only my first post

There are a lot of errors here, I rewrote the Server script for you:

local ranks = {"Player", "Associate", "Soldier", "Gang Leader", "Officer", "Senior Officer", "Chief Officer", "Commander", "Overseer", "Captain", "Major", "General", "Boss", "Right Hand Men", "Godfather"}

game:GetService("ReplicatedStorage"):WaitForChild("demote").OnServerEvent:Connect(function(user, target)
	-- user is a player object so you don't need to find the player
	local userrank = user:WaitForChild("leaderstats", 5).Rank -- value is a property, when you access a property it returns a COPY of the value, which won't change the object's value
	local usermoney = user.leaderstats.Money -- FindFirstChild unnecessary as WaitForChild ensured that it exists
	local targetp = game:GetService("Players"):FindFirstChild(tostring(target)) -- converts target into string in case it isn't
	if not targetp then warn("target doesn't exist!") return end -- checks if target actually exists
	local targetrank = targetp:FindFirstChild("leaderstats").Rank
	local userrankindex = table.find(ranks, userrank.Value)
	local targetrankindex = table.find(ranks, targetrank.Value)
	if not userrankindex or not targetrankindex then warn("rank not found") return end -- checks if ranks are found
	if targetrankindex < userrankindex and usermoney.Value >= 500 then do -- checks if player has enough as exploiters can fire this event freely
			local indexfinal = targetrankindex - 1
			local rankfinal = ranks[indexfinal]
			targetrank.Value = rankfinal
			usermoney.Value -= 500 --remove the cost
		end
	end
end)

There’s actually nothing wrong with that, though

1 Like

thanks for spending your time to write a detailed reply and it worked

and i will try to learn from my mistakes

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.