What do you want to achieve? run _G.function ONLY when a boolvalue is set to true.
What is the issue? It only seems to run when player respawns and it doesn’t even run when the boolvalue is clearly set to true.
Also when the player joins for the first time, an error saying attempt to call a nil value
The BoolValue is set to true by a serverscript.
What solutions have you tried so far? I tried adding a health function but it doesn’t seem to work too…
Both scripts are localscripts!
Only the server-script involved is the script that changes the boolvalue to true.
-- _G FUNCTION
_G.Ragdolled = function()
if values:WaitForChild("Ragdolled").Value == true then
warn("worked")
return true
elseif values:WaitForChild("Ragdolled").Value == false then
return false
end
end
-- RECEIVES _G FUNCTION
if _G.Ragdolled() then
print("hi")
end
Despite the boolvalue being set to true (by server-script) it doesn’t seem to print “hi”.
For starters, I wouldn’t recommend using _G at all, just use normal modules.
If this is a BoolValue (though I recommend trying Attributes) then you can use the .Changed event.
Something like the following:
values.Ragdolled.Changed:Connect(function(newValue)
if newValue then
print("It is true!")
else
print("It is false!")
end
end
I would like to also point out to not use :WaitForChild() each time, it yields your script. If you know it is there, you can use either :FindFirstChild, or just referencing it
To expand on why you shouldn’t use _G in this case (and many others) is because you are reliant on a “race condition” where you must set _G.Ragdolled = in a script before using _G.Ragdolled() in another script. but you do not specify an order explicitly so lua may do it in the correct order or not. Using a module script and require solves this, forcing Ragdolled to be set by reading the module script.
How would a modulescript work exactly for this scenario?
I know it uses functions just like _G, but do i just paste in the same script i used for _G.Ragdolled = function()?
Hello sorry for the late response, i was trying to figure out modulescripts and they seem to work fine but the same issue i’m having still recurs. the module script function is only called when player respawns and not when the value is set to true.
here are the scripts.
-- MODULESCRIPT
local module = {}
local player = game.Players.LocalPlayer.Character
local value = player:FindFirstChild("Values"):FindFirstChild("Ragdolled")
function module.Ragdolled()
if value.Value == true then
return true
elseif value.Value == false then
return false
end
end
return module
-- LOCALSCRIPT
local module = require(game.ReplicatedStorage.ModuleScript)
if module.Ragdolled() then
print("hi") -- Only prints when player respawns, which i don't want.
end
Yeah that happens because your script is in StarterCharacterScripts. The script is run from top to bottom every time the player is created. first it gets the module, then it checks if they are ragdolled, when the script ends it does not run again.
If you want to do something when something else happens you have to use events and connections. @noahrepublic’s first response is what you are looking for.
I guess so, i was originally using remote events that fires when player is ragdolled anyways, but they seem to be unreliable, but i’ll just stick to them for now on until i find a solution.