Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Code will run in Script but not Local Script? What can be the cause?

Asked by
DBoi941 30
2 months ago

I run the game with this in a script I get an error for it needs to be a localscript because I am firing a RemoteEvent. In a local script nothing happen at all when player touches the part.

local rS = game:GetService("ReplicatedStorage")

local Reward10k = rS.RemoteEvents:WaitForChild("Reward10k")

local Reward20k = rS.RemoteEvents:WaitForChild("Reward20k")



math.randomseed(tick())

script.Parent.Touched:connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

script.Disabled = true

local player = hit.Parent.Name

local num = 0

for i = 1,45 do

wait()

script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )

script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )

num = num + 8

end

num = 0



local random = math.random(1,100)

if random > 0 and random < 50 then

Reward10k:FireServer()

print("10K")

elseif random > 50 and random < 100 then

Reward20k:FireServer()

print("20K")

end

script.Parent:Destroy()

end

end)
0
Presumably your issue is that the LocalScript will not run from the Workspace, if it is placed there, move it into the StarterGui and manually input the location of the object that is currently the "script.Parent" also, your random function doesnt account for the numbers 50 or 100 SerpentineKing 2724 — 2mo
0
Yes the local script was in workplace. The reason I did that was because the part that will be touched will not always be in workplace. It spawns at random time so it will not be in workplace all the time so I can not just say local part = game.workspace DBoi941 30 — 2mo
0
Use :WaitForChild() then to make sure it exists, ex. local part = workspace:WaitForChild("PartNameHere") SerpentineKing 2724 — 2mo
0
Also you can't fire a remote event like that in a server script, use :FireAllCients(), then on the script that takes the event has to be a local script, and on that one you use .OnClientEvent Babyseal1015 30 — 2mo
0
Ok thank you. DBoi941 30 — 2mo

Answer this question